JAVA:
WebView wv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv = (WebView)findViewById(R.id.webView); wv.getSettings().setJavaScriptEnabled(true); wv.addJavascriptInterface(this, "nativeMethod"); wv.loadUrl("file:///android_asset/index.html"); } @JavascriptInterface public void toActivity(String activityName) { //此处应该定义常量对应,同时提供给web页面编写者 if(TextUtils.equals(activityName, "a")){ startActivity(new Intent(this,AActivity.class)); }else{ startActivity(new Intent(this,BActivity.class)); } }
JS:
<!DOCTYPE HTML> <html> <script type="text/javascript"> function gotoActivity (activity) { nativeMethod.toActivity(activity); } </script> <body> <button onClick="gotoActivity('a')">gotoActivity A</button> <button onClick="gotoActivity('b')">gotoActivity B</button> </body> </html>
转自http://blog.csdn.net/wangkai0681080/article/details/50178507