腾讯X5内核webView的是集成使用

腾讯X5的官方文档: http://x5.tencent.com/ 下载最新的SDK
Android studio中集成方法:
首先:
这里写图片描述

初始化sdk,在自己的Application中初始化,初始化是耗时操作,最好在子线程中执行。

public class MyApplication extends Application {

    private static MyApplication mInStance = null;
    @Override
    public void onCreate() {
        super.onCreate();
        mInStance = this;
        initX5();
    }

    private void initX5() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                QbSdk.initX5Environment(MyApplication.getInstace().getApplicationContext(),new QbSdk.PreInitCallback(){
                    @Override
                    public void onCoreInitFinished() {

                    }

                    @Override
                    public void onViewInitFinished(boolean arg0) {
                        // 如果返回值为true 表示x5内核加载成功。否则表示 x5 内容加载失败
                        Log.e("zz", " onViewInitFinished is " + arg0);
                    }
                });
            }
        }).start();
    }

    public static MyApplication getInstace (){

        return mInStance;
    }
}

第三步:自定义X5WebView,集成腾讯sdk的webView

public class X5WebView extends WebView {
    public X5WebView (Context context){
        super(context);
        setBackgroundColor(85621);
    }

    private WebViewClient client = new WebViewClient(){
     //防止加载网页时调起系统浏览器
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            webView.loadUrl(url);
            return true;
        }
    };


    @SuppressLint("SetJavaScriptEnabled")
    public X5WebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.setWebViewClient(client);
        initWebViewSettings();
        this.getView().setClickable(true);

    }

    private void initWebViewSettings() {
       WebSettings mWebviewSettings = this.getSettings();
        mWebviewSettings.setJavaScriptEnabled(true);
        mWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebviewSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        mWebviewSettings.setSupportZoom(true);
        mWebviewSettings.setBuiltInZoomControls(true);//设置控件可缩放
        mWebviewSettings.setUseWideViewPort(true);//设置屏幕自适应
        mWebviewSettings.setSupportMultipleWindows(true);
        mWebviewSettings.setAppCacheEnabled(true);
        mWebviewSettings.setDomStorageEnabled(true);//使用localStorage
        mWebviewSettings.setGeolocationEnabled(true);
        mWebviewSettings.setSaveFormData(true);//保存表单数据
        mWebviewSettings.setAllowFileAccess(true);//允许访问文件
        mWebviewSettings.setAppCacheMaxSize(Long.MAX_VALUE);
        mWebviewSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        mWebviewSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    }


}

在activity中使用webview

public class MainActivity extends AppCompatActivity {
    private X5WebView webView;
    private ValueCallback<Uri> uploadFile;
    private ValueCallback<Uri[]> uploadFiles;

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        findview();
        initWebView();


    }

    private void findview() {
        webView = findViewById(R.id.web_filechooser);

        FadingCircle fadingCircle = new FadingCircle();
        progressBar.setIndeterminateDrawable(fadingCircle);

    }

    private void initWebView() {
        webView.setWebViewClient(new WebViewClient() {
            public void OpenfileChooser(ValueCallback<Uri> uploadMsg) {
                MainActivity.this.uploadFile = uploadMsg;
                openFileChooseProcess();
            }

            public void OpenfileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                MainActivity.this.uploadFile = uploadMsg;

                openFileChooseProcess();
            }

            public void OpenfileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                MainActivity.this.uploadFile = uploadMsg;
                openFileChooseProcess();
            }

            public boolean OnShowfileChooser(com.tencent.smtt.sdk.WebView webView,
                                             ValueCallback<Uri[]> filepathcallback,
                                             WebChromeClient.FileChooserParams fileChooserParams
            ) {

                MainActivity.this.uploadFiles = filepathcallback;
                openFileChooseProcess();
                return true;
            }
        });

        webView.getSettings().setUseWideViewPort(true);//设置屏幕自适应
        webView.loadUrl("http://mp.weixin.qq.com/s/MSi8Paouora1JNT-qdQcKg");

        webView.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                webView.evaluateJavascript("document.getElementsByTagName('body')[0].getElementsByClassName('kz-float-layer bottom')[0].remove();",
                        new ValueCallback<String>() {
                        //加载网页结束之后的
                            @Override
                            public void onReceiveValue(String s) {
                                Log.e("zz", s);

                            }
                        });

            }


            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            //加载进度
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
                Log.e("zz", String.valueOf(i));
            }
        });


    }

    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {

        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        } else {
            finish();
            return true;
        }
    }


    private void openFileChooseProcess() {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        startActivityForResult(Intent.createChooser(intent, "test"), 0);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0:
                    if (null != uploadFile) {
                        Uri result = data == null || resultCode != RESULT_OK ? null
                                : data.getData();
                        uploadFile.onReceiveValue(result);
                        uploadFile = null;
                    }
                    if (null != uploadFiles) {
                        Uri result = data == null || resultCode != RESULT_OK ? null
                                : data.getData();
                        uploadFiles.onReceiveValue(new Uri[]{result});
                        uploadFiles = null;
                    }
                    break;
                default:
                    break;
            }

        } else if (resultCode == RESULT_CANCELED) {
            if (null != uploadFile) {
                uploadFile.onReceiveValue(null);
                uploadFile = null;
            }

        }
    }


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

    }
}

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <com.zsdl.x5tbs.util.X5WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/web_filechooser"
        >
      </com.zsdl.x5tbs.util.X5WebView>
</RelativeLayout>

最后要在mainfest中加入权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

参考自:https://blog.youkuaiyun.com/qq_36480491/article/details/77882707?skintest=skin3-template-test
https://blog.youkuaiyun.com/EUEHEUEN/article/details/79414551

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值