尊重原创:https://blog.csdn.net/nihaomabmt/article/details/80347945
H5调用APP的方法——APP来实现一些功能
1、通过下面的方法将webview的接口传给H5
- @param object the Java object to inject into this WebView's JavaScript
- * @param name the name used to expose the object in JavaScript
- webview.addJavascriptInterface(Object object, String name));
webview.addJavascriptInterface(new JsAction(), "action");
2、在JsAction类中添加相应的接口方法
android原生提供了一种注解方式@JavascriptInterface。只要在JsAction的类里面定义的方法都加上@JavascriptInterface即可。
- private class JsAction {
- @JavascriptInterface //H5调用Android的该方法
- public void toast(final String jsonString) {
- //注意@JavascriptInterface 下的该方法和当前activity并不是同一个线程,所以增加要将H5的调用和回调到H5的代码都写到runOnUiThread。否则会抛出以下异常
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- JSONObject object = null;
- try {
- object = new JSONObject(jsonString);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle("H5调用Android显示对话框");
- if (object != null) {
- builder.setMessage(object.optString("message", ""));
- }
- builder.setNegativeButton(android.R.string.ok, null);
- AlertDialog dialog = builder.create();
- dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
- @Override
- public void onDismiss(DialogInterface dialog) {
- //在dialog消息的时候,将修改H5的内容
- reloadJs("alert", "iOS change javascript to message for H5");
- }
- });
- dialog.show();
- }
- });
- }
- }
在H5调用的方法中添加如下代码即可
- //H5调用Android的方法来显示dialog
- function toast(){
- window.action.toast("{\"message\":\"From H5 message\"}");
- }
@JavascriptInterface 下的该方法和当前activity并不是同一个线程,所以增加要将H5的调用和回调到H5的代码都写到runOnUiThread。否则会抛出以下异常
- 05-17 16:11:00.847 13875-14010/com.j1 E/AndroidRuntime: FATAL EXCEPTION: JavaBridge
- Process: com.j1, PID: 13875
- java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 2) {839b89} called on Looper (JavaBridge, tid 349) {a0a3877}, FYI main Looper is Looper (main, tid 2) {839b89})
- at android.webkit.WebView.checkThread(WebView.java:2486)
- at android.webkit.WebView.loadUrl(WebView.java:938)
- at com.j1.MainActivity.reloadJs(MainActivity.java:89)
- at com.j1.MainActivity.access$100(MainActivity.java:18)
- at com.j1.MainActivity$JsAction$1.onDismiss(MainActivity.java:76)
- at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1361)
- at android.os.Handler.dispatchMessage(Handler.java:105)
- at android.os.Looper.loop(Looper.java:164)
- at android.os.HandlerThread.run(HandlerThread.java:65)
- Caused by: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 2) {839b89} called on Looper (JavaBridge, tid 349) {a0a3877}, FYI main Looper is Looper (main, tid 2) {839b89})
- at android.webkit.WebView.checkThread(WebView.java:2476)
- at android.webkit.WebView.loadUrl(WebView.java:938)
- at com.j1.MainActivity.reloadJs(MainActivity.java:89)
- at com.j1.MainActivity.access$100(MainActivity.java:18)
- at com.j1.MainActivity$JsAction$1.onDismiss(MainActivity.java:76)
- at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1361)
- at android.os.Handler.dispatchMessage(Handler.java:105)
- at android.os.Looper.loop(Looper.java:164)
- at android.os.HandlerThread.run(HandlerThread.java:65)