I wanted to modify one input value in the html after loading the webpage. I prepared to do this by calling javascript function with the WebView. It worked well if I set the android:targetSdkVersion to 18. But once I set it to 19, it shows the value of the second url, even it's just the javascript calls. Here's test code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebActivity extends Activity {
private WebView mWebView;
private String mJSCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_webview);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (mJSCall != null) {
view.loadUrl(mJSCall);
mJSCall = null;
}
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
mWebView.loadUrl("http://wap.soso.com/?g_f=2405");
String keyword = "My Keyword";
mJSCall = "javascript:document.getElementById('keyword').value='"+keyword+"'";
super.onCreate(savedInstanceState);
}
}
Could you tell me why it failed to call javascript function when targetSdkVersion=19? It seems that when the onPageFinished method is called the first url already refreshed sometimes, maybe that's why I could not modify the value in the html at that time.
So is there any right time to call javascript except the onPageFinished? I have struggled for the last day to find a reasonable solution. If anyone can help, please do. Thank you!