TBS 腾讯浏览服务 集成

腾讯浏览服务-首页  http://x5.tencent.com/

官方文档不多,不过实例比较详细,配置和谷歌的Webview差不多,Webview有的方法,TBS也都有。

jar包不到300KB,但是它需要X5内核,需要另外下载,不过如果手机装了微信QQ,就可以用它们的了。

 

把下载jar包添加到项目

在Application添加

public class App extends Application {

    @Override
    public void onCreate() {

        super.onCreate();

        // 搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
        // TbsDownloader.needDownload(getApplicationContext(), false);

        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                Log.e("apptbs", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                // TODO Auto-generated method stub

            }
        };
        QbSdk.setTbsListener(new TbsListener() {
            @Override
            public void onDownloadFinish(int i) {
                Log.d("apptbs", "onDownloadFinish");
            }

            @Override
            public void onInstallFinish(int i) {
                Log.d("apptbs", "onInstallFinish");
            }

            @Override
            public void onDownloadProgress(int i) {
                Log.d("apptbs", "onDownloadProgress:" + i);
            }
        });

        QbSdk.initX5Environment(getApplicationContext(), cb);

    }

}

 

MainActivity    布局全是按钮

public class MainActivity extends Activity {

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(MainActivity.this, WebActivity.class);
    }

    public void btn(View v) {
        switch (v.getId()) {

        case R.id.btn1:
            intent.putExtra("url", "https://www.huxiu.com/article/168405.html");
            startActivity(intent);
            break;
        case R.id.btn2:
            intent.putExtra("url", "http://feizaojilao.esy.es/history.html");
            startActivity(intent);
            break;
        case R.id.btn3:
            intent.putExtra("url", "http://v.qq.com/cover/z/z1x68ih8qwjbsen.html");
            startActivity(intent);
            break;
        case R.id.btn4:
            intent.putExtra("url", "http://v.youku.com/v_show/id_XMTc3NTIzNjY2NA==.html?spm=a2hww.20023042.m_223465.5~5~5~5!2~5~5~A&from=y1.3-idx-beta-1519-23042.223465.4-1");
            startActivity(intent);
            break;
        case R.id.btn5:
            intent.putExtra("url", "http://www.hao123.com/");
            startActivity(intent);
            break;
        case R.id.btn6:
            intent.putExtra("url", "http://www.apta.gov.cn/Information/ActivityDetail?aid=167");
            startActivity(intent);
            break;
        case R.id.btn7:
            intent.putExtra("url", "http://220.180.239.166/Information/ActivityDetail?aid=123");
            startActivity(intent);
            break;

        default:
            break;
        }

    }

}

 

WebActivity

public class WebActivity extends Activity {

    WebView tbsWebView;
    ProgressBar web_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        String url = getIntent().getStringExtra("url");

        web_bar = (ProgressBar) findViewById(R.id.web_bar);
        web_bar.getProgressDrawable().setColorFilter(Color.RED,
                android.graphics.PorterDuff.Mode.SRC_IN);

        tbsWebView = (WebView) findViewById(R.id.tbsWebView);
        tbsWebView.loadUrl(url);

        WebSettings settings = tbsWebView.getSettings();
        settings.setJavaScriptEnabled(true);

        // 设置加载进来的页面自适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(true);
        
        
        //settings.setUserAgent( );

        tbsWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("WebView ", "shouldOverrideUrlLoading " + url);
                view.loadUrl(url);
                return true;
            }

        });

        tbsWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                web_bar.setProgress(newProgress);

                if (newProgress == 100) {
                    web_bar.setVisibility(View.GONE);
                } else {
                    web_bar.setVisibility(View.VISIBLE);
                }
            }

        });
    }

    @Override
    protected void onDestroy() {
        if (tbsWebView != null)
            tbsWebView.destroy();
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && tbsWebView.canGoBack()) {
            tbsWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

 

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tsbdemo.WebActivity" >

    <com.tencent.smtt.sdk.WebView
        android:id="@+id/tbsWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/web_bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-7dp"
        android:layout_marginTop="-7dp"
        android:indeterminate="false" />

</RelativeLayout>

 

 

简单测试了一下,没有什么问题,感觉应该不会差,毕竟微信QQ内置的Webview都是这个

 

参考

性能 http://x5.tencent.com/guide?id=4000

Crosswalk 开发浅析,忘掉那些Webview的坑 - sslinp的博客 - 博客频道 - CSDN.NET  http://blog.csdn.net/sslinp/article/details/51607237

Android WebView常见问题及解决方案汇总 - Whatever is worth doing is worth doing well. - 博客频道 - CSDN.NET  http://blog.csdn.net/t12x3456/article/details/13769731

QQ浏览器X5内核问题汇总  https://www.qianduan.net/qqliu-lan-qi-x5nei-he-wen-ti-hui-zong/

 

posted @ 2016-10-26 17:49  demon9  阅读(9360)  评论(0编辑  收藏  举报