请问一个问题:
我按照作者的指示操作,
//Android 端 , AndroidInterface 是一个注入类 ,里面有一个无参数方法:callAndroid
mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface(mAgentWeb,this));
//在 Js 里就能通过
window.android.callAndroid() //调用 Java 层的 AndroidInterface 类里 callAndroid 方法
目的使用js调用我的java代码。但是我同事使用window.android.callAndroid()在H5中找不到方法,无法完成调用。是不是需要H5做一些什么操作才能行??
我的java代码是这样的:
public class AndroidInterface {
private Handler deliver = new Handler(Looper.getMainLooper());
private AgentWeb agent;
private Context context;
private ICallAndroidListener listener;
public AndroidInterface(AgentWeb agent, Context context, ICallAndroidListener listener) {
Toast.makeText(context.getApplicationContext(), "AndroidInterface" , Toast.LENGTH_LONG).show();
this.agent = agent;
this.context = context;
this.listener = listener;
}
@JavascriptInterface
public void callAndroid(final String msg) {
deliver.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context.getApplicationContext(), "" + msg, Toast.LENGTH_LONG).show();
listener.onAndroidCallbackListener();
}
});
}
}
Activity中
mAgentWeb = AgentWeb.with(this)//
.setAgentWebParent(reWebContainer, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))//传入AgentWeb的父控件
.useDefaultIndicator()
.defaultProgressBarColor()
.setAgentWebSettings(WebDefaultSettingsManager.getInstance())//设置 AgentWebSettings
.setWebViewClient(mWebViewClient)//WebViewClient , 与WebView 一样
.setWebChromeClient(mWebChromeClient) //WebChromeClient
// .setPermissionInterceptor(mPermissionInterceptor) //权限拦截
.setReceivedTitleCallback(mCallback)//标题回调
.setSecutityType(AgentWeb.SecurityType.strict) //严格模式
// .addDownLoadResultListener(mDownLoadResultListener) //下载回调
// .openParallelDownload()//打开并行下载 , 默认串行下载
// .setNotifyIcon(R.mipmap.download)
.createAgentWeb()//创建AgentWeb
.ready()//设置 WebSettings
.go(H5_Url); //WebView载入该url地址的页面并显示。
//在这里进行 H5 对 Android原生的调用
private void jsAndroid() {
//Android 端 , AndroidInterface 是一个注入类 ,里面有一个无参数方法:callAndroid
mAgentWeb.getJsInterfaceHolder().addJavaObject("android", new AndroidInterface(mAgentWeb, this, new ICallAndroidListener() {
@override
public void onAndroidCallbackListener() {
//关闭activity
finish();
}
}));
}