11-06 18:29:15.582:w/webview (27807): Java.lang.throwable:a WebView method was called on thread ' Javabridge '. All WebView methods must is called on the same thread. (Expected Looper Looper (Main, Tid 1) {425f48a8} called on Looper (Javabridge, Tid 92104) {426508d0}, FYI main Looper is L Ooper (Main, Tid 1) {425f48a8})
Today, a friend in the group came in to ask this question, I prompted him: The exception information hint is very clear, all the WebView method than the call must be in a thread, 2 methods call Tid is obviously different.
Let's look at the code he wrote first:
Package Com.webview;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.os.Handler;import Android.webkit.javascriptinterface;import Android.webkit.webview;public class Mainactivity extends Activity {private Handler handler= New Handler ();p rivate WebView WebView; @SuppressLint ("Newapi") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); WebView = (WebView ) Findviewbyid (R.id.webview);//Find Webviewwebview.getsettings (). Setjavascriptenabled (true) by ID;// Allow Jswebview.loadurl ("file:///android_asset/index.html");//Load page webview.addjavascriptinterface (new Contact (), " Contact ");//Create contact object, pass to WebView, as JS object}class contact {@JavascriptInterfacepublic void showcontacts () {/* Handler.post (New Runnable () {@Overridepublic void run () {String json = "[{name:\" Wangxiao \ ", amount:\" 12345\ ", phone:\" 18600012345\ "}, {name:\ "Dawn \", amount:\ "54321\", Phone:\ "18600054321\"}] "; Webview.loadurl (" Javascript:show (' "+ JSON +" ') ");//Call JS method// To pass the JS data to the HTML page}); */string json = "[{name:\" Wangxiao \ ", amount:\" 12345\ ", Phone:\" 18600012345\ "}, {name:\" Dawn \ ", Amount:\" 54321\ ", phone:\" 18600054321\ "}]", Webview.loadurl ("Javascript:show ('" + JSON + ")");//Call JS method//The JS data, passed to the HTML page}@ javascriptinterfacepublic void Call (final String phone) {/*handler.post (new Runnable () {@Overridepublic void run () { StartActivity (New Intent (Intent.action_call, Uri.parse ("tel://" + Phone));}); */startactivity (New Intent (Intent.action_call, Uri.parse ("tel://" + Phone));}} }
The above code in fact in android4.4 the following version will not be any problem, I have written before, but Android 4.1,api 17, that is, Jelly_bean started, Android for WebView in the execution of the JS code and native code between the interaction made some changes, the specific changes I did not go to research, just to follow the exception information to give a solution;
The above code, as long as the JS called Method call () and showcontacts () are placed on the same handler.post thread execution is no problem.
In addition, only the public method identified by the Javascriptinterface annotation can be accessed by the JS code , we must remember this
@JavascriptInterfacepublic void Showcontacts () {
If in the android4.1 above version, you provide to the JS call method, does not have the annotated annotation, this method cannot be accessed by the JavaScript in the WebView, many people also asked this question.
Look at the final execution effect:
Index.html code in the assets directory:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Source code Download: (Someone needs me to upload)
android4.1 Jelly_bean:all WebView methods must being called on the same thread[problem solved]