两篇超详细的良心攻略
https://www.jianshu.com/p/3c94ae673e2a web view的api介绍
https://www.jianshu.com/p/345f4d8a5cfa java js互调
java调用js
准备一个html文件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Carson_Ho</title> <script> function callJS(){ alert("Android调用了JS的callJS方法"); } </script> </head> </html>
调用
public class MainActivity extends AppCompatActivity { private static final String TAG = "xbh"; private WebView wv; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FrameLayout fl = findViewById(R.id.fl); wv = new WebView(this); Button bt = findViewById(R.id.bt); fl.addView(wv); WebSettings ws = wv.getSettings(); ws.setJavaScriptEnabled(true); ws.setJavaScriptCanOpenWindowsAutomatically(true); wv.loadUrl("file:///android_asset/javascript.html"); wv.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { Log.i(TAG, "LoadFinished: "); view.loadUrl("javascript:callJS()"); } }); wv.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.i(TAG, "onJsAlert: " + url + " " + message + " " + result); return true; } }); } }
也可以用evaluateJavascript()-》4.4以上用这个方法
js调用java
1.addJavascriptInterface()
js代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Carson</title> <script> function callAndroid(){ test.hello("js调用了android中的hello方法"); } </script> </head> <body> </body> </html>
java
package com.example.demo; import android.annotation.SuppressLint; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.webkit.JavascriptInterface; import android.webkit.JsResult; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "xbh"; private WebView wv; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FrameLayout fl = findViewById(R.id.fl); wv = new WebView(this); fl.addView(wv); wv.loadUrl("file:///android_asset/javascript.html"); wv.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { view.loadUrl("javascript:callAndroid()"); } }); WebSettings ws = wv.getSettings(); ws.setJavaScriptEnabled(true); wv.addJavascriptInterface(new AndroidtoJs(), "test");//AndroidtoJS类对象映射到js的test对象 } class AndroidtoJs{ @JavascriptInterface public void hello(String msg) { Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); Log.i(TAG, msg); } } }
但是有漏洞
2.shouldOverrideUrlLoading拦截url
3.拦截alert()、confirm()、prompt()
还有就是用JsBridge等框架