WebView addJavascriptInterface在某些设备中不起作用,并且在某些具有相同版本5.1.1的设备中工作

时间:2017-03-29 11:04:37

标签: javascript java android webview

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setLightTouchEnabled(true);
    webView.getSettings().setJavaScriptEnabled(true);

    final JavaScriptInterface myJavaScriptInterface = new           JavaScriptInterface(this);
        webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
        Log.e("debug", "checkNetwork() called Node URL : " + ConstantResource.NODE_URL);

        NODE_IFS_URL = ConstantResource.NODE_URL + ConstantResource.IFS_NODE + "=" + strIfs + "_" + strAndroidId;
        webView.loadUrl(NODE_IFS_URL);

    WebViewClient MyWebViewClient = new WebViewClient()
    {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(Uri.parse(url).getScheme().equalsIgnoreCase("http://dummy.dummyurl.com/"))  {
                // This is a trusted site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            // This is by default the Android Browser.
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    };}

在onCreate中我已经声明了webView。

public class JavaScriptInterface {
    Context mContext;


    JavaScriptInterface(Context c) {
        mContext = c;
    }

   @JavascriptInterface
    public void showToast(String webMessage) {

           Toast.makeText(mContext,webMessage,Toast.LENGTH_LONG).show();
                Log.e("Message 1 : ", strSocket);

    }}

我已经创建了JavaScriptInterface但是这在某些设备上不起作用,并且在具有相同Android版本的某些设备中工作。 我的minSdk和targetSDK也设置为17。

0 个答案:

没有答案