首页 > 解决方案 > Android webview 中的 JavaScript 注入

问题描述

我已经编写了包含回调处理程序的 Javascript 代码到我的 android 应用程序并从我的 android 代码中注入它。当我们在 webview 中执行视频的播放/暂停操作并在回调中返回事件时,它会触发回调事件。我们正在从两个平台(iOS 和 Android)注入 Javascript 代码

但不幸的是,在 iOS 中,所有事件值数据都在获取,但在 Android 中显示“未定义”

下面是我从 Android 注入的 JavaScript 代码

webView.addJavascriptInterface(new Object() {
            @JavascriptInterface
            public void getEvent(String data) {
                Toast.makeText(getContext(),    data , Toast.LENGTH_SHORT).show();

            Log.w("TAGGG", "callBackEvent: " + data.toString() );

        }
    }, "abc");

@Override
            public void onPageFinished(WebView view, String url) {
                tvErrorMessage.setVisibility(View.INVISIBLE);
                String scriptThree = "javascript:window.myApp_event =(function() {" +
                        "function notifyApp(event) {" +
                        "var myCallback = console.log(event);"+
                        "abc.getEvent(event);" + // This is callback for Android
                        "}" +
                        "var oExistingEvents = window.myApp_event || [];" +
                        "oExistingEvents.forEach(notifyApp);" +
                        "return {" +
                        "push: function(event) {" +
                        "notifyApp(event);" +

                        "}" +
                        "}" +
                        "})();";
                webView.loadUrl(scriptThree);
            }

我们如何获取在 iOS 中获取但在 Android 中没有的事件名称?

标签: javascriptandroidandroid-webview

解决方案


推荐阅读