WebView中实现javaScript和Android互调

创建时间:2017/10/22 13:41:17
关键字:JavascriptInterface,addJavascriptInterface,loadUrl

关键代码:

webview.addJavascriptInterface(new MyJavaScript(this), "AndroidNative");

<a href='javascript:AndroidNative.telephone("13816978870")'>

webview.loadUrl("javascript:**");
 
定义@JavascriptInterface 接口的类
 
public class AndroidNative { 
    Context context;
    public  AndroidNative(Context context) { 
        this.context = context; 
    } 
  
    @JavascriptInterface 
    public void telephone(String phone) { 
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" phone)); 
        context .startActivity(intent); 
   } 
}