实现内嵌在app中的webview 采用腾讯开源X5 高效安全


webview在app的使用中,十分频繁,原生的webview加载速度相对来说很慢,而且很费流量。腾讯开源了x5的webview jar包,无疑是好事,毕竟经过了过亿的用户量的使用,性能还是值得赞的。

譬如咱们来加载百度页面




官方的网址为:

腾讯x5  http://x5.tencent.com/doc?id=1004

调用

private WebView X5WebView;//使用腾讯X5WebView
private String url = "http://wap.baidu.com";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view_test);
    X5WebView = (com.tencent.smtt.sdk.WebView)findViewById(R.id.tbsContent);
    X5WebView.loadUrl(url);
    WebSettings webSettings = X5WebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    X5WebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && X5WebView.canGoBack()) {
        X5WebView.goBack();// 返回前一个页面
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

布局

<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">
    <com.tencent.smtt.sdk.WebView
        android:id="@+id/tbsContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>




Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐