Android使用腾讯浏览服务X5内核
【前期准备】
腾讯X5 jar包下载地址 [点击打开]
本次完整DEMO源码 [打开Github]
【集成步骤】
第一步:下载jar包添加到项目
第二步:添加权限
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
第三步:创建BaseApplication.class
- import android.app.Application;
- import com.tencent.smtt.sdk.QbSdk;
- import com.tencent.smtt.sdk.TbsListener;
- import com.xiong.tbs_x5.utils.LogUtil;
- /**
- * Created by xiongwenwei@aliyun.com
- * CreateTime: 2017/1/12
- * Note:BaseApplication
- */
- public class BaseApplication extends Application {
- private static BaseApplication instance;
- @Override
- public void onCreate() {
- super.onCreate();
- instance = this;
- initTbs();
- }
- private void initTbs() {
- //搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
- QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
- @Override
- public void onViewInitFinished(boolean arg0) {
- LogUtil.i("onViewInitFinished is " + arg0);
- }
- @Override
- public void onCoreInitFinished() {
- }
- };
- QbSdk.setTbsListener(new TbsListener() {
- @Override
- public void onDownloadFinish(int i) {
- LogUtil.i("onDownloadFinish");
- }
- @Override
- public void onInstallFinish(int i) {
- LogUtil.i("onInstallFinish");
- }
- @Override
- public void onDownloadProgress(int i) {
- LogUtil.i("onDownloadProgress:" + i);
- }
- });
- QbSdk.initX5Environment(getApplicationContext(), cb);
- }
- public static BaseApplication getInstance() {
- return instance;
- }
- }
不要忘记添加:
第三步:创建tbs_activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <com.tencent.smtt.sdk.WebView
- android:id="@+id/webView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/white" />
- </LinearLayout>
第四步:创建TbsActivity.class
- import android.graphics.PixelFormat;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.WindowManager;
- 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.xiong.tbs_x5.R;
- /**
- * Created by xiongwenwei@aliyun.com
- * CreateTime: 2017/1/13
- * Note:腾讯浏览服务
- */
- public class TbsActivity extends AppCompatActivity{
- private com.tencent.smtt.sdk.WebView webView;
- private String url = "http://3g.qq.com";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- getWindow().setFormat(PixelFormat.TRANSLUCENT);//(这个对宿主没什么影响,建议声明)
- getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- initView();
- loadUrl(url);
- }
- private void initView() {
- webView = (com.tencent.smtt.sdk.WebView) findViewById(R.id.webView);
- }
- private void loadUrl(String url) {
- WebSettings webSettings = webView.getSettings();
- webSettings.setJavaScriptEnabled(true);
- webView.loadUrl(url);
- webView.setWebViewClient(new WebViewClient() {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.loadUrl(url);
- return true;
- }
- @Override
- public void onReceivedError(WebView var1, int var2, String var3, String var4) {
- Log.i("打印日志","网页加载失败");
- }
- });
- //进度条
- webView.setWebChromeClient(new WebChromeClient() {
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- if (newProgress == 100) {
- Log.i("打印日志","加载完成");
- }
- }
- });
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (webView != null) webView.destroy();
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (webView != null && webView.canGoBack()) {
- webView.goBack();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- return super.onKeyDown(keyCode, event);
- }
- }
【接入成功效果】
上述代码只是最基础的集成,更多的效果,请移步官网 [官网接入文档]
大概试了下,APP第一次启动好像用的还是自带Webview,猜测是X5内核还没下载完吧
接入成功,标志:1.长按文字会出现"保存离线网页" 2.复制英文会出现"翻译" 3.复制文字会有水滴标志
posted on 2018-01-23 09:59 cn三少<script></script> 阅读(894) 评论(0) 编辑 收藏 举报