1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| package com.fei.bh.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import android.webkit.ConsoleMessage; import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebResourceError; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient;
import com.fei.bh.R; import org.json.JSONException; import org.json.JSONObject;
public class StarActivity extends AppCompatActivity {
private static final String TAG = "StarActivity";
private String url; WebView webView;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_star);
webView = findViewById(R.id.star_webView); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setDomStorageEnabled(true); settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setAllowFileAccess(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setLoadsImagesAutomatically(true); settings.setDefaultTextEncodingName("utf-8");
webView.addJavascriptInterface(StarActivity.this, "androidFei");
webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { view.loadUrl(request.getUrl().toString()); return true; }
@Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); Log.e(TAG, "执行__加载开始 " + url); }
@Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); Log.e(TAG, "执行__加载完成 " + url);
webView.post(new Runnable() { @Override public void run() { webView.evaluateJavascript("javascript:callJsFunction('hello js')", new ValueCallback<String>() {
@Override public void onReceiveValue(String s) { Log.d(TAG, "js返回的结果: " + s); } }); } }); }
@Override public void onLoadResource(WebView view, String url) { super.onLoadResource(view, url); Log.e(TAG, "执行__加载资源 " + url); }
@Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(webView, request, error); Log.e(TAG, "执行__错误了"); } });
webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { super.onConsoleMessage(consoleMessage); Log.e(TAG, "执行__输出js中的console.log 的内容 " + consoleMessage.message()); return true; } });
url = "file:///android_asset/h5/index.html#/home"; webView.loadUrl(url);
}
@JavascriptInterface public void saveFei() { System.out.println(TAG + "andFei: 安卓我收到了vue中请求了"); Log.d(TAG, "andFei: 安卓我接受到vue中的值了"); }
@JavascriptInterface public void saveFeiArgs1(String args) { Log.d(TAG, "andFei: 安卓我接受到vue中的值了" + args); }
@JavascriptInterface public void saveFeiArgs2(String json) throws JSONException { Log.d(TAG, "andFei: 安卓我接受到vue中的所有值了" + json); JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); String age = jsonObject.getString("age");
Log.d(TAG, "andFei: 安卓我接受到vue中的值了: " + name); Log.d(TAG, "andFei: 安卓我接受到vue中的值了: " + age); }
}
|