activity对应layout文件加入:
<WebView
android:id="@+id/main_web"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
activity代码 :
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWeb = (WebView)findViewById(R.id.main_web); mWeb.getSettings().setJavaScriptEnabled(true); //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 mWeb.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return true; } });
以下为5.1机器上测试,js无法调用未导出的组件方法,
hello为导出的类对象。不知道大神是用的什么方法调起来的
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script type="text/javascript"> function test() { ob1=window.hello if(ob1!=null) { cls=ob1.getClass() if(cls==null) { return } rt=cls.forName("java.lang.Runtime") if(rt!=null) { rt.getMethod("getRuntime").invoke().exec("echo 111"); } } } </script> <title></title> </head> <body> <input type="button" value="返回安卓的某一个界面" onclick="test()"/> </body> </html>