Adding JavaScript Interface to a Browser

后端 未结 2 1524
小鲜肉
小鲜肉 2020-12-06 13:59

Is it possible to add a JavaScript interface to the Android Browser the same way one can be added to the WebView Component as illustrated in this demo. My particular use ca

相关标签:
2条回答
  • 2020-12-06 14:29

    You can do it using jsinterface. First you need to make the browser jsinterface enabled and then you may call to Android method from the HTML of your browser.

    You may , get a fair example and idea here ...

    http://android-puremvc-ormlite.blogspot.com/2011/07/using-java-script-in-android-webview.html

    0 讨论(0)
  • 2020-12-06 14:50

    You can invoke methods and functions in your webview by using javascript url's, e.g.

    webview.loadUrl("javascript:somemethod()");
    

    You will, of course, need to enable javascript on your webview:

    webview.getSettings().setJavaScriptEnabled(true);
    

    This is from java to javascript. If you want to invoke java code / android API's from javascript, use addJavascriptInterface()

    webview.addJavascriptInterface(new MyJSJavaBridge(), "api");
    

    All of this is shown in the example url you posted as well.

    0 讨论(0)
提交回复
热议问题