zoukankan      html  css  js  c++  java
  • 腾讯X5WebView集成及在移动端中使用

    工作中经常涉及H5网页的加载工作,最多使用的就是安卓系统控件WebView,但是当网页内容比较多的时候,需要等待很久才能加载完,加载完后用户才能看到网页中的内容,这样用户需要等很久,体验很差。

    那能不能边加载边显示呢,通过搜索发现腾讯X5WebView可以实现,相对体验要好很多,况且手Q、微信、QQ浏览器使用的该插件,故值得一试。

    步骤如下:

    一、下载jar包及so文件分别放到libs和jniLibs文件夹


     二、添加权限

        <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" />


    三、Application中初始化

            private void initX5WebView() {
                //搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
                QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
                    @Override
                    public void onViewInitFinished(boolean arg0) {
                        //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
                        Log.d("app", " onViewInitFinished is " + arg0);
                    }
         
                    @Override
                    public void onCoreInitFinished() {
                    }
                };
                //x5内核初始化接口
                QbSdk.initX5Environment(getApplicationContext(), cb);
            }


    四、使用自定义X5WebView继承腾讯包下WebView

        import android.annotation.SuppressLint;
        import android.content.Context;
        import android.util.AttributeSet;
         
        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;
         
        public class X5WebView extends WebView {
         
            private WebViewClient client = new WebViewClient() {
                /**
                 * 防止加载网页时调起系统浏览器
                 */
                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.setWebChromeClient(chromeClient);
                // WebStorage webStorage = WebStorage.getInstance();
                initWebViewSettings();
                this.getView().setClickable(true);
            }
         
            private void initWebViewSettings() {
                WebSettings webSetting = this.getSettings();
                webSetting.setJavaScriptEnabled(true);
                webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
                webSetting.setAllowFileAccess(true);
                webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
                webSetting.setSupportZoom(true);
                webSetting.setBuiltInZoomControls(true);
                webSetting.setUseWideViewPort(true);
                webSetting.setSupportMultipleWindows(true);
                // webSetting.setLoadWithOverviewMode(true);
                webSetting.setAppCacheEnabled(true);
                // webSetting.setDatabaseEnabled(true);
                webSetting.setDomStorageEnabled(true);
                webSetting.setGeolocationEnabled(true);
                webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
                // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
                webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
                // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
                webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
         
                // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
                // settings 的设计
            }
         
            public X5WebView(Context arg0) {
                super(arg0);
                setBackgroundColor(85621);
            }
         
        }


    五、Activity中使用

         
        public class WebViewTestActivity extends Activity {
         
            private static final String mHomeUrl = "http://app.html5.qq.com/navi/index";
            private X5WebView mX5WebView;
         
            @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_webview_test);
                initHardwareAccelerate();
                initView();
            }
         
            /**
             * 启用硬件加速
             */
            private void initHardwareAccelerate() {
                try {
                    if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                        getWindow()
                                .setFlags(
                                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
                    }
                } catch (Exception e) {
                }
            }
            
            private void initView() {
                mX5WebView = findViewById(R.id.x5_webview);
                mX5WebView.loadUrl(mHomeUrl);
            }
         
            /**
             * 返回键监听
             * @param keyCode
             * @param event
             * @return
             */
            @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (mX5WebView != null && mX5WebView.canGoBack()) {
                        mX5WebView.goBack();
                        return true;
                    } else {
                        return super.onKeyDown(keyCode, event);
                    }
                }
                return super.onKeyDown(keyCode, event);
            }
         
           
            @Override
            protected void onDestroy() {
                //释放资源
                if (mX5WebView != null)
                    mX5WebView.destroy();
                super.onDestroy();
            }
        }


    六、Xml文件

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
         
            <net.edaibu.testapplication.activity.webview.X5WebView
                android:id="@+id/x5_webview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
         
        </RelativeLayout>


    七、腾讯X5WebView接入文档地址

        https://x5.tencent.com/tbs/guide/sdkInit.html点击打开链接

    八、后续继续编辑

  • 相关阅读:
    selenium实战脚本集(2)——简单的知乎爬虫
    selenium实战脚本集(1)——新浪微博发送QQ每日焦点
    使用swift和rails来实现ios账号系统
    一段js代码
    你应该学会使用的5个ruby方法
    小而美的ghost driver
    还没被玩坏的robobrowser(8)——robobrowser的实现原理
    还没被玩坏的robobrowser(7)——表单操作
    还没被玩坏的robobrowser(6)——follow_link
    还没被玩坏的robobrowser(5)——Beautiful Soup的过滤器
  • 原文地址:https://www.cnblogs.com/lyfankai/p/10607533.html
Copyright © 2011-2022 走看看