webView专题内容Android收藏集WebView

腾讯X5WebView集成使用

2018-04-23  本文已影响144人  世道无情
  1. 概述

在开发过程中,如果涉及到加载网页我们之前使用的是Android系统的控件WebView,现在有一种可以起同样的作用、同样的效果就是腾讯的X5WebVIew,可以加载网页、播放网页视频,支持全屏、半屏,适用于网页比较多的时候,可以边加载边显示,体验也还不错,下边就介绍下它的具体用法。

2. 具体使用


1>:下载jar包,然后复制到自己项目的libs下,并且创建jniLibs目录,复制armeabi的so文件:


图片.png

2>:在app下的 build.gradle下复制:


图片.png
3>:使用IntentService在BaseApplication中初始化
/**
 * Email: 2185134304@qq.com
 * Created by JackChen 2018/4/23 11:45
 * Version 1.0
 * Params:
 * Description:
*/
public class HomeApplication extends Application {

    private static HomeApplication homeApplication;
    @Override
    public void onCreate() {
        super.onCreate();
        homeApplication=this;
        // 初始化X5WebView
        preInitX5Core();
    }

    public static HomeApplication getInstance(){
        return homeApplication;
    }

    private void preInitX5Core() {
        //预加载x5内核
        Intent intent = new Intent(this, X5NetService.class);
        startService(intent);
    }
}

4>:在IntentService的onHandleIntent()方法中初始化:

/**
 * Email: 2185134304@qq.com
 * Created by JackChen 2018/4/23 11:46
 * Version 1.0
 * Params:
 * Description: 
*/
public class X5NetService extends IntentService {

    public static final String TAG = LogTAG.x5webview;
    public X5NetService(){
        super(TAG);
    }
    public X5NetService(String name) {
        super(TAG);
    }

    /**
     * 初始化X5WebView
     */
    @Override
    public void onHandleIntent(@Nullable Intent intent) {
        initX5Web();
    }
    public void initX5Web() {
        if (!QbSdk.isTbsCoreInited()) {
            // 设置X5初始化完成的回调接口
            QbSdk.preInit(getApplicationContext(), null);
        }
        QbSdk.initX5Environment(getApplicationContext(), cb);
    }

    QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
        @Override
        public void onViewInitFinished(boolean arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onCoreInitFinished() {
            // TODO Auto-generated method stub
        }
    };
}

5>:在布局文件中引用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">
    <cn.novate.x5webview.widget.MyX5WebView
        android:id="@+id/main_web"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </cn.novate.x5webview.widget.MyX5WebView>
</LinearLayout>

6>:加载X5WebView:

public class X5WebGameActivity extends BaseActivity {

    private MyX5WebView myX5WebView;
    private String webUrl;
    private long exitTime = 0;
    private Bundle extras;
    public final String TAG = LogTAG.x5webview;
    public static final String DATA = "确定退出当前页面吗?";
    @Override
    public int getLayoutId() {
        extras = getIntent().getExtras();
        webUrl = extras.getString("key");
        Logger.i(TAG,"load url : "+ webUrl );
        full(true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        return R.layout.layout_x5;
    }

    @Override
    public void initViews() {
        myX5WebView = $(R.id.main_web);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        myX5WebView.loadUrl(webUrl);
    }

    @Override
    public void processClick(View v) {

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(this,DATA,Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void full(boolean enable) {
        if (enable) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attr);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }
}

3. X5WebView优势:

图片.png

具体代码已上传至github:
https://github.com/shuai999/X5WebView.git

上一篇 下一篇

猜你喜欢

热点阅读