Android开发 -使用腾讯TBS浏览服务 X5webview控件
生活随笔
收集整理的這篇文章主要介紹了
Android开发 -使用腾讯TBS浏览服务 X5webview控件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<上一篇? Android 開發(fā)-webview實現(xiàn)打開本地頁面及交互
上一篇我們簡單的實現(xiàn)了Android 開發(fā)-webview實現(xiàn)打開本地頁面及交互,接下來,我們用TBS騰訊瀏覽服務(wù)(X5WebView)代替原生的webview控件。
1、首先,我們需要引用對應(yīng)的lib庫,可直接在上面官網(wǎng)查看接入方式,這里我是用的之前下載的lib庫(點擊到下載地址)進(jìn)行導(dǎo)入
導(dǎo)入lib中的jar庫2、然后我們創(chuàng)建一個APPAplication類,進(jìn)行預(yù)加載處理。
APPAplication.java 類
package com.example.demo;import android.app.Application; import android.util.Log;import com.tencent.smtt.sdk.QbSdk;public class APPAplication extends Application {@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {@Overridepublic void onViewInitFinished(boolean arg0) {// TODO Auto-generated method stub//x5內(nèi)核初始化完成的回調(diào),為true表示x5內(nèi)核加載成功,否則表示x5內(nèi)核加載失敗,會自動切換到系統(tǒng)內(nèi)核。Log.d("app", " onViewInitFinished is " + arg0);}@Overridepublic void onCoreInitFinished() {// TODO Auto-generated method stub}};//x5內(nèi)核初始化接口QbSdk.initX5Environment(getApplicationContext(), cb);} }3、接下來我們創(chuàng)建一個新的空Activity,X5WebviewActivity,在這個activity上進(jìn)行嵌套x5webview
4、自定義一個X5WebView控件
package com.example.demo.ui;import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView;import com.tencent.smtt.sdk.WebSettings; import com.tencent.smtt.sdk.WebView; import com.tencent.smtt.sdk.WebViewClient;public class X5WebView extends WebView {TextView title;private WebViewClient client = new WebViewClient() {/*** 防止加載網(wǎng)頁時調(diào)起系統(tǒng)瀏覽器*/public boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}};@SuppressLint("SetJavaScriptEnabled")public X5WebView(Context arg0, AttributeSet arg1) {super(arg0, arg1);this.setWebViewClient(client);this.getView().setClickable(true);}public X5WebView(Context arg0) {super(arg0);setBackgroundColor(85621);}}5、在新activity中嵌入x5webview
activity_x5_webview.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".X5WebviewActivity"><LinearLayoutandroid:id="@+id/lay_base"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/tv_info"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:visibility="gone" /><TextViewandroid:id="@+id/tv_info_type"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:visibility="gone" /><FrameLayoutandroid:id="@+id/webView1"android:layout_width="match_parent"android:layout_height="match_parent"></FrameLayout></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>X5WebviewActivity
package com.example.demo;import androidx.appcompat.app.AppCompatActivity;import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.webkit.JavascriptInterface; import android.webkit.JsPromptResult; import android.widget.FrameLayout; import android.widget.Toast;import com.example.demo.ui.X5WebView; import com.tencent.smtt.export.external.extension.interfaces.IX5WebViewExtension; import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient; import com.tencent.smtt.export.external.interfaces.JsResult; import com.tencent.smtt.export.external.interfaces.WebResourceError; import com.tencent.smtt.export.external.interfaces.WebResourceRequest; import com.tencent.smtt.sdk.CookieSyncManager; import com.tencent.smtt.sdk.DownloadListener; import com.tencent.smtt.sdk.ValueCallback; import com.tencent.smtt.sdk.WebChromeClient; import com.tencent.smtt.sdk.WebSettings; import com.tencent.smtt.sdk.WebView; import com.tencent.smtt.sdk.WebViewClient; import com.tencent.smtt.utils.TbsLog;import java.util.HashMap; import java.util.Map;import static java.lang.System.currentTimeMillis;public class X5WebviewActivity extends AppCompatActivity {private X5WebView mWebView;private ViewGroup mViewParent;private final int hand_js_doJsFunction = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_x5_webview);BindWebView();}private void BindWebView(){mViewParent = (ViewGroup) findViewById(R.id.webView1);IniteWebView();}private void IniteWebView(){mWebView = new X5WebView(this, null);//動態(tài)添加webviewmViewParent.addView(mWebView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.FILL_PARENT));//長按監(jiān)聽mWebView.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {return true;}});WebSettings settings = mWebView.getSettings();settings.setAllowFileAccess(true);settings.setAllowFileAccessFromFileURLs(true);settings.setAllowUniversalAccessFromFileURLs(true);settings.setJavaScriptEnabled(true);mWebView.loadUrl( "file:///android_asset/test.html");//本地文件//mWebView.loadUrl( "www.baidu.com");//網(wǎng)絡(luò)文件//定義給頁面js調(diào)用app的方法mWebView.addJavascriptInterface(appToJsObject, "AndroidJS");}//== webview 與js交互=========================//定義提供html頁面調(diào)用的方法public final Object appToJsObject = new Object() {@JavascriptInterfacepublic void GetAppInfo() {//獲取app信息String appInfo = getAppInfo();doJsFunction("backInfo('"+appInfo+"')");}};//定義公共方法調(diào)用頁面js方法public void doJsFunction(String _url){Message msg = new Message();msg.what = hand_js_doJsFunction;Bundle bundle = new Bundle();bundle.putString("url",_url); //往Bundle中存放數(shù)據(jù)msg.setData(bundle);//mes利用Bundle傳遞數(shù)據(jù)handler.sendMessage(msg);//用activity中的handler發(fā)送消息}//用handler訪問讓方法在主進(jìn)程內(nèi)處理Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {String[] args = (String[]) msg.obj;switch (msg.what) {case hand_js_doJsFunction:// String str1 = msg.getData().getString("url");//接受msg傳遞過來的參數(shù)//調(diào)用頁面js方法mWebView.loadUrl("javascript:"+msg.getData().getString("url"));break;default:break;}}};//獲取應(yīng)用信息public String getAppInfo() {String info = "";try {Context mContext = getApplicationContext();PackageManager packageManager = mContext.getPackageManager();PackageInfo packageInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);info += "版本號:" + packageInfo.versionName + "\n";info += "包 名:" + packageInfo.packageName + "\n";} catch (Exception e) {e.printStackTrace();}return info;} }6、基本代碼與之前的內(nèi)置webview基本一致。
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Android开发 -使用腾讯TBS浏览服务 X5webview控件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言如何做一个打卡的程序,C语言实现学
- 下一篇: 联发科MT6763处理器参数MT6763