1.在Android中和和H5的的交互: ?Android中H5開發(fā)? ??Android和js交互
開發(fā)中存在native和H5界面之間的交互問題,例如點擊h5的界面彈出native組件,如toast。
使用傳統(tǒng)的JSinterface通信:?
Android界面:acticity加載webview,支持javascript
? ? WebSettings webSettings = webView.getSettings();
? ? webSettings.setJavaScriptEnabled(true);
然后通過WebView的addJavascriptInterface方法去注入一個我們自己寫的interface
? ? ? ? webView.addJavascriptInterface(newJsInterface(),"control");
? ? ? ? webView.loadUrl("file:///android_asset/interact.html");
html代碼中js的代碼:
functionshowToast(toast){
javascript:control.showToast(toast);
}
這里的control就是我們的那個interface,調(diào)用了interface的showToast方法
我們自己寫的interface
publicclassJsInterface{
@JavascriptInterface
publicvoidshowToast(String toast){
Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();
log("show toast success");
}