生活随笔
收集整理的這篇文章主要介紹了
Android Studio 嵌入X5WebView
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
制作好APP在真機上運行時發現頁面總是略大于屏幕,查詢網上的解決方法,通過Android原生webview設置分辨率啦、縮放啦、自適應等等,沒啥用。后來看到X5WebView很多人贊,行吧,我也試試看好了。
走攻略下載X5WebView項目,主要從demo項目中復制下圖對應文件夾下的文件到我的項目中來。
在gradle.properties中加入代碼
android.useDeprecatedNdk=true
在build.gradle中defaultConfig {}塊中寫入代碼
ndk{abiFilters "armeabi", "armeabi-v7a", "x86", "mips"}
然后呢,寫一個類來使用X5WebView,在這里就要寫好WebView導入了,不然會和Android元素WebView混一起。
我需要做一個內嵌網頁的登錄和登出,登錄成功記住賬號和密碼,登出的時候登錄頁不顯示注冊頁而是老用戶登錄頁。
package com
.example
.com
.hello1
;import android
.annotation
.SuppressLint
;
import android
.content
.ActivityNotFoundException
;
import android
.content
.Context
;
import android
.content
.Intent
;
import android
.graphics
.Bitmap
;
import android
.graphics
.Color
;
import android
.graphics
.Rect
;
import android
.text
.TextUtils
;
import android
.util
.AttributeSet
;
import android
.view
.View
;
import com
.tencent
.smtt
.sdk
.WebSettings
;
import com
.tencent
.smtt
.sdk
.WebSettings
.LayoutAlgorithm
;
import com
.tencent
.smtt
.sdk
.WebView
;
import com
.tencent
.smtt
.sdk
.WebViewClient
;
import java
.net
.URISyntaxException
;public class X5WebView extends WebView {static boolean isx5Reset
= false;static boolean delay
= false;static String x5url
= "";static boolean
IS_NEED_CLAER = false;private WebViewClient client
= new WebViewClient() {@Override
public boolean
shouldOverrideUrlLoading(WebView view
, String url
) {WebView
.HitTestResult hitTestResult
= view
.getHitTestResult();if (!TextUtils
.isEmpty(url
) && hitTestResult
== null) {return true;}if (shouldOverrideUrlLoadingByApp(view
, url
)) {return true;}return super.shouldOverrideUrlLoading(view
, url
);}@Override
public void onPageStarted(WebView view
, String url
, Bitmap favicon
) {super.onPageStarted(view
, url
, favicon
);}@Override
public void onPageFinished(WebView view
, String url
) {super.onPageFinished(view
, url
);if (url
.equals(MainActivity
.reurl
)) { if (MainActivity
.USERNAME != "") view
.loadUrl("javascript:(function() { document.getElementById('username').value='" + MainActivity
.USERNAME + "';"+ "document.getElementById('password').value='" + MainActivity
.PASSWORD + "';document.getElementsByClassName('Login')[0].style.display = 'block';document.getElementsByClassName('Register')[0].style.display = 'none';})()");}if (!delay
) {delay
= true;view
.setBackgroundColor(Color
.TRANSPARENT); view
.setVisibility(View
.VISIBLE); }if (url
.equals("退登網址")) {MainActivity
.getStr();view
.loadUrl(MainActivity
.reurl
);IS_NEED_CLAER = true;}}@Override
public void onReceivedError(WebView view
, int errorCode
, String description
, String failingUrl
) {super.onReceivedError(view
, errorCode
, description
, failingUrl
);if (!isx5Reset
) {isx5Reset
= true;x5url
= failingUrl
;view
.loadUrl("file:///android_asset/html/reset.html");}}@Override
public void doUpdateVisitedHistory(WebView view
, String url
, boolean isReload
) {super.doUpdateVisitedHistory(view
, url
, isReload
);if (IS_NEED_CLAER) {IS_NEED_CLAER = false;view
.clearHistory();}}};@
SuppressLint("SetJavaScriptEnabled")public X5WebView(Context arg0
, AttributeSet arg1
) {super(arg0
, arg1
);this.setWebViewClient(client
);initWebViewSettings();this.getView().setClickable(true);}private void initWebViewSettings() {WebSettings mWebSettings
= this.getSettings();mWebSettings
.setJavaScriptEnabled(true);mWebSettings
.setJavaScriptCanOpenWindowsAutomatically(true);mWebSettings
.setAllowFileAccess(true);mWebSettings
.setLayoutAlgorithm(LayoutAlgorithm
.NARROW_COLUMNS);mWebSettings
.setSupportZoom(true);mWebSettings
.setBuiltInZoomControls(true);mWebSettings
.setUseWideViewPort(true);mWebSettings
.setSupportMultipleWindows(true);mWebSettings
.setAppCacheEnabled(true);mWebSettings
.setDomStorageEnabled(true);mWebSettings
.setGeolocationEnabled(true);mWebSettings
.setAppCacheMaxSize(Long
.MAX_VALUE);mWebSettings
.setPluginState(WebSettings
.PluginState
.ON_DEMAND);mWebSettings
.setCacheMode(WebSettings
.LOAD_NO_CACHE);mWebSettings
.setMixedContentMode(android
.webkit
.WebSettings
.MIXED_CONTENT_ALWAYS_ALLOW);}public X5WebView(Context arg0
) {super(arg0
);setBackgroundColor(85621);}private boolean
shouldOverrideUrlLoadingByApp(WebView view
, String url
) {if (url
.startsWith("http") || url
.startsWith("https") || url
.startsWith("ftp")) {return false;}Intent intent
;try {intent
= Intent
.parseUri(url
, Intent
.URI_INTENT_SCHEME);} catch (URISyntaxException e
) {
return false;}intent
.setComponent(null);try {getContext().startActivity(intent
);} catch (ActivityNotFoundException e
) {return false;}return true;}
}
載入X5內核的初始化需要定義MyApplication類,且AndroidManifest.xml中application的name名就為MyApplication。首次打開APP就需要成功加載X5內核,失敗則用原生的WebView
package com
.example
.com
.hello1
;import android
.app
.Application
;
import android
.app
.Service
;
import android
.content
.Intent
;
import android
.os
.IBinder
;
import android
.support
.annotation
.Nullable
;import com
.tencent
.smtt
.sdk
.QbSdk
;public class MyApplication extends Application {@Override
public void onCreate() {super.onCreate();preinitX5WebCore();Intent intent
= new Intent(this, AdvanceLoadX5Service
.class);startService(intent
);}private void preinitX5WebCore() {if (!QbSdk
.isTbsCoreInited()) {QbSdk
.preInit(getApplicationContext(), null);}}public class AdvanceLoadX5Service extends Service {@Nullable@Override
public IBinder
onBind(Intent intent
) {return null;}@Override
public void onCreate() {super.onCreate();initX5();}private void initX5() {QbSdk
.initX5Environment(getApplicationContext(), cb
);}QbSdk
.PreInitCallback cb
= new QbSdk.PreInitCallback() {@Override
public void onViewInitFinished(boolean arg0
) {}@Override
public void onCoreInitFinished() {}};}
}
總結
以上是生活随笔為你收集整理的Android Studio 嵌入X5WebView的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。