android 4.4 webview,How to call JavaScript with Android 4.4 Webview?

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值