javascript - 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 中没有的事件名称?
解决方案
推荐阅读
- swift - 如何在 SwiftUI 中设置叠加层的不透明度
- python - Python:OpenCV:创建图像轮播
- r - 压缩 R 中分类变量的级别
- python - ValueError:即使在应用重复()之后也无法从重复的轴重新索引
- javascript - 如何选择下拉菜单中的一项?
- flutter - 不确定我家中是否有正确的空 .zshrc 空文件来配置颤振
- asp.net - OpenIdDict 多个授权服务器无法解码相同的访问令牌
- dataframe - 如何在 DataFrame(0.19 版)中删除带有特定单词的列?
- vue.js - 是否可以测试使用注入而不使用包装组件的 Vue 3 组合库?
- android-studio - 北极狐:安卓模拟器没有返回键