andriod端调用uni-app的js方法
- 安卓代码
mWebView.post(new Runnable() {
@Override
public void run() {
mWebView.loadUrl("javascript:closeLoading(\"" + message + "\")");
}
});
- uni-app端代码,要在onRead中注册js方法
onReady() {
window.closeLoading = (message) => {
this.showLoad = false;
uni.showToast({
title: message,
icon: "none",
duration: 2000
})
}
},
uni-app调用原生的安卓方法
- 在Activity中创建一个内部类
final class DemoJavaScriptInterface {
DemoJavaScriptInterface() {
}
@JavascriptInterface
public void closeLoad(String message) {
mWebView.post(new Runnable() {
@Override
public void run() {
mWebView.loadUrl("javascript:closeLoading(\"" + message + "\")");
}
});
}
}
- 在onCreate进行注册
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setMediaPlaybackRequiresUserGesture(true);
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "android");
mWebView.loadUrl("file:///android_asset/dist/index.html");
}
- uni-app端调用写法
window.android.closeLoading(params)