腾讯X5webview集成

1:在application中初始化:
private void initX5() {
    //搜集本地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() {
            // Log.d(("X5内核初始化完成");
        }
    };

    QbSdk.setTbsListener(new TbsListener() {
        @Override
        public void onDownloadFinish(int i) {

        }

        @Override
        public void onInstallFinish(int i) {

        }

        @Override
        public void onDownloadProgress(int i) {

        }
    });
    //x5内核初始化接口
    QbSdk.initX5Environment(getApplicationContext(), cb);
}

2:继承extends com.tencent.smtt.sdk.WebView自定义webview

package com.ebid.tendering.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

public class MyWebView extends com.tencent.smtt.sdk.WebView {


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


    @SuppressLint("SetJavaScriptEnabled")
    public MyWebView(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
        this.setWebViewClient(client);
        // this.setWebChromeClient(chromeClient);
        // WebStorage webStorage = WebStorage.getInstance();
        initWebViewSetting(arg0,this);
    }


    public static void initWebViewSetting(Context context, WebView webView) {
        if (webView == null) {
            return;
        }
        WebSettings webSettings = webView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //允许混合内容 解决部分手机 加载不出https请求里面的http下的图片
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        //设置自适应屏幕,两者合用
        webSettings.setJavaScriptEnabled(true);
        //启用数据库
        webSettings.setDatabaseEnabled(true);
        String dbPath = context.getApplicationContext().getDir("db",Context.MODE_PRIVATE).getPath();
        webSettings.setDatabasePath(dbPath);
        webSettings.setDefaultTextEncodingName("UTF-8");
        //设置定位的数据库路径
//        String dir = context.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
//        webSettings.setGeolocationDatabasePath(dir);
        //启用地理定位
//        webSettings.setGeolocationEnabled(true);
        //开启DomStorage缓存
        webSettings.setDomStorageEnabled(true);

        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setSupportZoom(false); // 支持缩放
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        String ua = webSettings.getUserAgentString();


//        webSettings.setBlockNetworkImage(true);
    }

    // Log.d(
}

3:webactivity展示信息定制内容

public class WebActivity extends BaseActivity<WebInteractionContrat.IPresenter> implements WebInteractionContrat.IView, TitleBar.OnTitleBarClickListener {


    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.web_view)
    MyWebView mWebView;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;


    private String mUrl;
    private String mTitle;

    private String userId;
    private String appId;


    @Override
    protected void initPresenter() {
        mPresenter=new WebInteractionPresenter(activity,this);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }


    @Override
    protected void initView() {
        super.initView();
        int statusBarHeight = StatusBarUtil.getStatusBarHeight(activity);
        mTitleBar.setViewheight(statusBarHeight);
        mTitleBar.setTitleBarClick(this);


    }


    @Override
    public void initData() {
        initWebData();

        mTitle = getIntent().getStringExtra("title");
        mUrl = getIntent().getStringExtra("url");
        mTitleBar.setTitle(mTitle);

        mWebView.loadUrl(mUrl);

    }

    @Override
    public void onLeftClick() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }

    @Override
    public void onRightClick() {

    }


    /**
     * 登录成功后,初始化网页
     */
    private void initWebData() {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebContentsDebuggingEnabled(true);
        mWebView.addJavascriptInterface(new JavaScriptInterface(), "ChinaBidding");

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String s) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
            }
        });

    }


    class JavaScriptInterface {

        JavaScriptInterface() {

        }


        /**
         * js调原生 --- 通知,保存支付结果信息
         */
        @JavascriptInterface
        public void onSavePayRecordInfo(String goodsName, String userId, String platformCode, String payWay, double amount,
                                        String mcOrderNo, String payOrderNo, String goodsId, String goodsType, String payStatus) {
            mPresenter.savePayRecordInfo(goodsName, userId, platformCode, payWay, amount, mcOrderNo, payOrderNo, goodsId, goodsType, payStatus);
        }

        /**
         * js调原生 --- 通知,保存服务消息信息
         */
        @JavascriptInterface
        public void onSaveServiceMsgInfo(String userId, String msgUrl, String msgName, String msgDetail, String msgIcon,
                                         String msgType, String msgTitle, String msgComefrom, String isNowSend, String isLink) {
            mPresenter.saveServiceMsgInfo(userId, msgUrl, msgName, msgDetail, msgIcon, msgType, msgTitle, msgComefrom, isNowSend, isLink);
        }

        /**
         * js调原生 --- 应用授权登录
         */
        @JavascriptInterface
        public void onAuthAppLogin() {
            mPresenter.authAppLogin(userId, appId);
        }

        /**
         * js调原生 --- 根据应用的权限,获取用户基本信息
         */
        @JavascriptInterface
        public void onUserInfoByAppAuthType(String openId, String authType) {
            mPresenter.getUserInfoByAppAuthType(userId, openId, appId, authType);
        }

    }


    @Override
    public void vsavePayRecordInfo(WebInteractionBean body, String state) {
        if ("0".equals(state)) {
            if (body.header.returnCode.equals("0")) {
                ToastUtil.show(context, body.header.msg);
            } else {
                ToastUtil.show(context, body.header.msg);
            }
        } else {
            ToastUtil.show(context, R.string.please_service_exception);
        }
    }


    @Override
    public void vsaveServiceMsgInfo(WebInteractionBean body, String state) {
        if ("0".equals(state)) {
            if (body.header.returnCode.equals("0")) {
                ToastUtil.show(context, body.header.msg);
            } else {
                ToastUtil.show(context, body.header.msg);
            }
        } else {
            ToastUtil.show(context, R.string.please_service_exception);
        }
    }


    @Override
    public void vgetUserInfoByAppAuthType(WebInteractionBean body, String state) {
        if ("0".equals(state)) {
            if (body.header.returnCode.equals("0")) {
                String userInfo = body.data.userInfo;
                //将 授权信息 传给 网页端
                mWebView.loadUrl("javascript:userInfoByAppAuthTypeResult(" + userInfo + ")");
            } else {
                ToastUtil.show(context, body.header.msg);
            }
        } else {
            ToastUtil.show(context, R.string.please_service_exception);
        }
    }


    @Override
    public void vauthAppLogin(WebInteractionBean body, String state) {
        if ("0".equals(state)) {
            if (body.header.returnCode.equals("0")) {
                String openId = body.data.openId;
                //将 openId 传给 网页端
                mWebView.loadUrl("javascript:authAppLoginResult(" + openId + ")");
            } else {
                ToastUtil.show(context, body.header.msg);
            }
        } else {
            ToastUtil.show(context, R.string.please_service_exception);
        }
    }



    private void showAuthDialog(){
        AuthDialog authDialog =new AuthDialog(WebActivity.this);
        authDialog.show();

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值