Android-WebView4.4以下兼容

嵌入tbs.x5會被Google Play移除應用,請謹慎使用

問題描述

App開發時或多或少加載web頁面,但是使用h5或者一些新特性來寫的web頁在Android低版本系統上經常出現各種兼容適配問題。

TbsBridgeWebView使用

考慮App用戶羣的極少數沒裝有微信、手Q的情況,因此採用TbsX5 for share。下文基於Tbs for share來實現。

之前寫過一篇Android-使用JsBridge來優化js與本地的交互的文章,這次的TbsBridgeWebView同樣集成了這套JsBridge,同時使用TbsX5解決web適配問題。

添加maven依賴

<dependency>
<groupId>com.hjhrq1991.library.tbs</groupId>
<artifactId>tbsjsbridge</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>

添加gradle依賴

compile 'com.hjhrq1991.library.tbs:tbsjsbridge:1.0.0'

添加權限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

在你的佈局上添加TbsBridgeWebView

<com.hjhrq1991.library.tbs.TbsBridgeWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

JsBridge的使用請參考Android-使用JsBridge來優化js與本地的交互

wvWebview.setWebViewClient(new MyWebViewClient());
wvWebview.setVerticalScrollBarEnabled(false); //垂直不顯示
wvWebview.setHorizontalScrollBarEnabled(false);//水平不顯示
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
{
    WebSettings webSettings = wvWebview.getSettings();
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持內容重新佈局
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用緩存
    webSettings.setJavaScriptEnabled(true);//允許JS
    webSettings.setUseWideViewPort(true);//設置此屬性,可任意比例縮放
    webSettings.setLoadWithOverviewMode(true);//設置屏幕自適應

}
wvWebview.loadUrl(url);

注:Android 4.4以下(不包含4.4)系統WebView底層實現是採用WebKit內核  19以下不需要設置Setting,但這裏Api==18時也要設置Setting要不無法自適應屏幕

重要Tips

Tbs替換android.webkit相同的類

#系統內核                                       #SDK內核
android.webkit.ConsoleMessage                 com.tencent.smtt.export.external.interfaces.ConsoleMessage
android.webkit.CacheManager                   com.tencent.smtt.sdk.CacheManager(deprecated)
android.webkit.CookieManager                  com.tencent.smtt.sdk.CookieManager
android.webkit.CookieSyncManager              com.tencent.smtt.sdk.CookieSyncManager
android.webkit.CustomViewCallback             com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback
android.webkit.DownloadListener               com.tencent.smtt.sdk.DownloadListener
android.webkit.GeolocationPermissions         com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback
android.webkit.HttpAuthHandler                com.tencent.smtt.export.external.interfaces.HttpAuthHandler
android.webkit.JsPromptResult                 com.tencent.smtt.export.external.interfaces.JsPromptResult
android.webkit.JsResult                       com.tencent.smtt.export.external.interfaces.JsResult
android.webkit.SslErrorHandler                com.tencent.smtt.export.external.interfaces.SslErrorHandler
android.webkit.ValueCallback                  com.tencent.smtt.sdk.ValueCallback
android.webkit.WebBackForwardList             com.tencent.smtt.sdk.WebBackForwardList
android.webkit.WebChromeClient                com.tencent.smtt.sdk.WebChromeClient
android.webkit.WebHistoryItem                 com.tencent.smtt.sdk.WebHistoryItem
android.webkit.WebIconDatabase                com.tencent.smtt.sdk.WebIconDatabase
android.webkit.WebResourceResponse            com.tencent.smtt.export.external.interfaces.WebResourceResponse
android.webkit.WebSettings                    com.tencent.smtt.sdk.WebSettings
android.webkit.WebSettings.LayoutAlgorithm    com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm
android.webkit.WebStorage                     com.tencent.smtt.sdk.WebStorage
android.webkit.WebView                        com.tencent.smtt.sdk.WebView
android.webkit.WebViewClient                  com.tencent.smtt.sdk.WebViewClient

關於Cookie

com.tencent.smtt.sdk.CookieManager和com.tencent.smtt.sdk.CookieSyncManager的相關接口的調用,在接入SDK後,需要放到創建X5的WebView之後(也就是X5內核加載完成)進行;否則,cookie的相關操作只能影響系統內核。

參考文章

1.主流瀏覽器內核介紹(前端開發值得了解的瀏覽器內核歷史 
2.Android各個版本WebView 
3.第三方瀏覽器內核嵌入:Crosswalk 
4.Tbs接入文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章