Hello, I am developing an android application that sends an sms message to a certain number depending on what is dispayed in the webview. The text in the site that is loaded when the app starts is as follows: YES 0772334556 VOTE ME
Here is part of BrowserActivity.java (excluding sms methods)
......
public class BrowserActivity extends Activity {
String url = "http://192.241.208.86/zimjobs/zim.php?sn=";
static String serial = null;
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
// if statement, sms should be sent if YES
if(getData() == true){
for (int i=0; i < 3; i++)
{
Toast.makeText(getApplicationContext(),
url + getDeviceId(), Toast.LENGTH_LONG).show();
}
// SMS method goes here. SMS is sent
}
else{
for (int i=0; i < 3; i++)
{
Toast.makeText(getApplicationContext(),
"SMS not Sent", Toast.LENGTH_LONG).show();
// SMS not sent
}
}
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl(url + getDeviceId());
webview.requestFocus();
}
/** Reads content from web page
* @return */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public boolean getData(){ // Method not working
String text = "YES";
webview.findAll(text);
webview.findAllAsync(text);
return true;
}
// Get device serial number
public static String getDeviceId() {
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serial = (String) get.invoke(c, "ro.serialno");
} catch (Exception ignored) {
}
return serial;
}
.....
Now in the above code I have included the webview.findAll(text) and webview.findAllAsync(text) in getData() and used it in the onCreate method to find the "YES" string in page loaded in webview but it seems not to work.
All I want to do is to get certain text from the webview and maybe store the string in the code (using arrays, json or something else), then assign the string to a variable. For example I want to get the YES text(as well 0772334556 and VOTE ME) from the webview, store it in a variable and then use the variable in an if statement to determine whether the sms should be sent or not.
I have looked into using HTTPClient methods and javascript to get the html source code as well as create an AsyncTask class but they all look messy. I have included INTERNET and READ_PHONE_STATE permissions in the android manifest for internet and serial number access respectively, as well as WebView in activity_browser.xml.
Any help would be greatly appreciated.