首先在xml中引入控件
<com.github.lzyzsd.jsbridge.BridgeWebView
android:id="@+id/xqWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在activity
bridgeWebView = (BridgeWebView) findViewById(R.id.xqWebView);
WebSettings websettings = bridgeWebView.getSettings();
websettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
websettings.setLoadsImagesAutomatically(true);
websettings.setJavaScriptEnabled(true);
websettings.setUseWideViewPort(true);
websettings.setSupportZoom(true);
websettings.setSupportMultipleWindows(true);
websettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
websettings.setBuiltInZoomControls(true);
websettings.setSupportZoom(true);
websettings.setJavaScriptCanOpenWindowsAutomatically(true);
websettings.setAppCacheEnabled(true);
websettings.setDomStorageEnabled(true);
1、setSupportZoom(boolean support)
設定WebView是否支援使用螢幕控件或手勢進行縮放,預設是true,支援縮放。
getSettings.setSupportZoom(false);
2、setMediaPlaybackRequiresUserGesture(boolean require)
設定WebView是否通過手勢觸發播放媒體,預設是true,需要手勢觸發。
getSettings.setMediaPlaybackRequiresUserGesture(false);
3、setBuiltInZoomControls(boolean enabled)
設定WebView是否使用其内置的變焦機制,該機制集合螢幕縮放控件使用,預設是false,不使用内置變焦機制。
getSettings.setBuiltInZoomControls(true);
4、setDisplayZoomControls(boolean enabled)
設定WebView使用内置縮放機制時,是否展現在螢幕縮放控件上,預設true,展現在控件上。
getSettings.setDisplayZoomControls(false);
5、setAllowFileAccess(boolean allow)
設定在WebView内部是否允許通路檔案,預設允許通路。
getSettings.setAllowFileAccess(false);
6、setAllowContentAccess(boolean allow)
設定WebView是否使用其内置的變焦機制,該機制結合螢幕縮放控件使用,預設是false,不使用内置變焦機制。
getSettings.setAllowContentAccess(false);
7、setLoadWithOverviewMode(boolean overview)
設定WebView是否使用預覽模式加載界面。
getSettings.setLoadWithOverviewMode(false);
8、setSaveFormData(boolean save)
設定WebView是否儲存表單資料,預設true,儲存資料。
getSettings.setSaveFormData(false);
9、setTextZoom(int textZoom)
設定WebView中加載頁面字型變焦百分比,預設100,整型數。
getSettings.setTextZoom(100);
10、setAcceptThirdPartyCookies(boolean accept)
設定WebView通路第三方Cookies政策,參考CookieManager提供的方法:setShouldAcceptThirdPartyCookies。
getSettings.setAcceptThirdPartyCookies(false);
11、setUseWideViewPort(boolean use)
設定WebView是否使用viewport,當該屬性被設定為false時,加載頁面的寬度總是适應WebView控件寬度;當被設定為true,目前頁面包含viewport屬性标簽,在标簽中指定寬度值生效,如果頁面不包含viewport标簽,無法提供一個寬度值,這個時候該方法将被使用。
getSettings.setUseWideViewPort(false);
12、setSupportMultipleWindows(boolean support)
設定WebView是否支援多屏視窗,參考WebChromeClient#onCreateWindow,預設false,不支援。
getSettings.setSupportMultipleWindows(true);
13、setLayoutAlgorithm(LayoutAlgorithm l)
設定WebView底層的布局算法,參考LayoutAlgorithm#NARROW_COLUMNS,将會重新生成WebView布局
getSettings.setLayoutAlgorithm(LayoutAlgorithm l);
14、setStandardFontFamily(String font)
設定WebView标準字型庫字型,預設字型“sans-serif”。
getSettings.setStandardFontFamily("sans-serif");
15、setFixedFontFamily(String font)
設定WebView固定的字型庫字型,預設“monospace”。
getSettings.setFixedFontFamily("monospace");
16、setSansSerifFontFamily(String font)
設定WebView Sans SeriFontFamily字型庫字型,預設“sans-serif”。
getSettings.setSansSerifFontFamily("sans-serif");
17、setSerifFontFamily(String font)
設定WebView seri FontFamily字型庫字型,預設“sans-serif”。
getSettings.setSansSerifFontFamily("sans-serif");
18、setCursiveFontFamily(String font)
設定WebView字型庫字型,預設“cursive”
getSettings.setCursiveFontFamily("cursive");
19、setFantasyFontFamily(String font)
設定WebView字型庫字型,預設“fantasy”。
getSettings.setFantasyFontFamily("fantasy");
20、setMinimumFontSize(int size)
設定WebView字型最小值,預設值8,取值1到72
getSettings.setMinimumFontSize(8);
21、setMinimumLogicalFontSize(int size)
設定WebView邏輯上最小字型值,預設值8,取值1到72
getSettings.setMinimumLogicalFontSize(8);
22、setDefaultFontSize(int size)
設定WebView預設值字型值,預設值16,取值1到72
getSettings.setDefaultFontSize(16);
23、setDefaultFixedFontSize(int size)
設定WebView預設固定的字型值,預設值16,取值1到72
getSettings.setDefaultFixedFontSize(16);
24、setLoadsImagesAutomatically(boolean flag)
設定WebView是否加載圖檔資源,預設true,自動加載圖檔
getSettings.setLoadsImagesAutomatically(false);
25、setBlockNetworkImage(boolean flag)
設定WebView是否以http、https方式通路從網絡加載圖檔資源,預設false
getSettings.setBlockNetworkImage(true);
26、setBlockNetworkLoads(boolean flag)
設定WebView是否從網絡加載資源,Application需要設定通路網絡權限,否則報異常
getSettings.setBlockNetworkLoads(true);
27、setJavaScriptEnabled(boolean flag)
設定WebView是否允許執行JavaScript腳本,預設false,不允許
getSettings.setJavaScriptEnabled(true);
28、setAllowUniversalAccessFromFileURLs(boolean flag)
設定WebView運作中的腳本可以是否通路任何原始起點内容,預設true
getSettings.setAllowUniversalAccessFromFileURLs(false);
29、setAllowFileAccessFromFileURLs(boolean flag)
設定WebView運作中的一個檔案方案被允許通路其他檔案方案中的内容,預設值true
getSettings.setAllowFileAccessFromFileURLs(false);
30、setGeolocationDatabasePath(String databasePath)
設定WebView儲存地理位置資訊資料路徑,指定的路徑Application具備寫入權限
getSettings.setGeolocationDatabasePath(String path);
31、setAppCacheEnabled(boolean flag)
設定Application緩存API是否開啟,預設false,設定有效的緩存路徑參考setAppCachePath(String path)方法
getSettings.setAppCacheEnabled(true);
32、setAppCachePath(String appCachePath)
設定目前Application緩存檔案路徑,Application Cache API能夠開啟需要指定Application具備寫入權限的路徑
getSettings.setAppCachePath(String appCachePath);
33、setDatabaseEnabled(boolean flag)
設定是否開啟資料庫存儲API權限,預設false,未開啟,可以參考setDatabasePath(String path)
getSettings.setDatabaseEnabled(false);
34、setDomStorageEnabled(boolean flag)
設定是否開啟DOM存儲API權限,預設false,未開啟,設定為true,WebView能夠使用DOM storage API
getSettings.setDomStorageEnabled(true);
35、setGeolocationEnabled(boolean flag)
設定是否開啟定位功能,預設true,開啟定位
getSettings.setGeolocationEnabled(false);
36、setJavaScriptCanOpenWindowsAutomatically(boolean flag)
設定腳本是否允許自動打開彈窗,預設false,不允許
getSettings.setJavaScriptCanOpenWindowsAutomatically(true);
37、setDefaultTextEncodingName(String encoding)
設定WebView加載頁面文本内容的編碼,預設“UTF-8”。
getSettings.setDefaultTextEncodingName("UTF-8");
38、setUserAgentString(String ua)
設定WebView代理字元串,如果String為null或為空,将使用系統預設值
getSettings.setUserAgentString(String ua);
39、setNeedInitialFocus(boolean flag)
設定WebView是否需要設定一個節點擷取焦點當被回調的時候,預設true
getSettings.setNeedInitialFocus(false);
40、setCacheMode(int mode)
重寫緩存被使用到的方法,該方法基于Navigation Type,加載普通的頁面,将會檢查緩存同時重新驗證是否需要加載,如果不需要重新加載,将直接從緩存讀取資料,允許用戶端通過指定LOAD_DEFAULT、LOAD_CACHE_ELSE_NETWORK、LOAD_NO_CACHE、LOAD_CACHE_ONLY其中之一重寫該行為方法,預設值LOAD_DEFAULT
getSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
41、setMixedContentMode(int mode)
設定當一個安全站點企圖加載來自一個不安全站點資源時WebView的行為,android.os.Build.VERSION_CODES.KITKAT預設為MIXED_CONTENT_ALWAYS_ALLOW,android.os.Build.VERSION_CODES#LOLLIPOP預設為MIXED_CONTENT_NEVER_ALLOW,取值其中之一:MIXED_CONTENT_NEVER_ALLOW、MIXED_CONTENT_ALWAYS_ALLOW、MIXED_CONTENT_COMPATIBILITY_MODE.
getSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
出處:https://www.jianshu.com/p/0d7d429bd216
若從h5接受消息:
bridgeWebView.registerHandler("loginAction", new BridgeHandler() {
@Override
public void handler(String data, CallBackFunction function) {
if (data.equals("hello")) {
Intent intent = new Intent(ActivityCarXQ.this, ActivityCarXqJgt.class);
startActivity(intent);
} else {
Toast.makeText(ActivityCarXQ.this, "未從網絡擷取列印資料!", Toast.LENGTH_LONG).show();
}
}
});
發送消息給h5:
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("c_oem_brand", title);
} catch (JSONException e) {
e.printStackTrace();
}
bridgeWebView.callHandler("registerAction", jsonObject.toString(), new CallBackFunction() {
@Override
public void onCallBack(String data) {
}
});
其中第一個字段需和h5約定一緻