weex集成至android项目

如何将weex集成至已有项目

1.在app level的build.gradle中添加依赖库

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.alibaba:fastjson:1.1.57.android'
    compile 'com.taobao.android:weex_sdk:0.11.0@aar'

2.实现IWXImgLoaderAdapter接口

package reed.demoweex;

import android.widget.ImageView;

import com.taobao.weex.adapter.IWXImgLoaderAdapter;
import com.taobao.weex.common.WXImageStrategy;
import com.taobao.weex.dom.WXImageQuality;

/**
 * Created by thinkreed on 2017/5/26.
 */

public class ImageAdapter implements IWXImgLoaderAdapter {
    @Override
    public void setImage(String url, ImageView view, WXImageQuality quality, WXImageStrategy strategy) {

    }
}

3.实现一个WXApplication类

package reed.demoweex;

import android.app.Application;

import com.taobao.weex.InitConfig;
import com.taobao.weex.WXSDKEngine;

/**
 * Created by thinkreed on 2017/5/26.
 */

public class WXApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        InitConfig config = new InitConfig.Builder().setImgAdapter(new ImageAdapter()).build();
        WXSDKEngine.initialize(this, config);
    }
}

4.选择MainActivity作为weex的远程渲染页面

package reed.demoweex;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.taobao.weex.IWXRenderListener;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.common.WXRenderStrategy;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements IWXRenderListener {


    private static final int REQUEST_CODE_FOR_MODIFY_INTERNET = 0;
    private static final int REQUEST_CODE_FOR_ACCESS_NETWORK_STATE = 1;
    private static String TEST_URL = "http://dotwe.org/raw/dist/6fe11640e8d25f2f98176e9643c08687.bundle.js";
    WXSDKInstance mWXSDKInstance;
    private FrameLayout mContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkApiVersionAndPermission();

        mContainer = (FrameLayout) findViewById(R.id.container);

        mWXSDKInstance = new WXSDKInstance(this);
        mWXSDKInstance.registerRenderListener(this);
        /**
         * 第一个参数pageName:自定义的String类型包名,这里命名为demoweex
         * 第二个参数url:远程bundle js文件既.we 文件经过transform之后的js文件的下载地址,这里使用weex官方提供的示例
         * 第三个参数options:初始化时传入WEEX的参数,比如 bundle JS文件地址
         * 第四个参数jsonInitData,可传null值
         * 第五个参数flag:渲染策略。WXRenderStrategy.APPEND_ASYNC:异步策略先返回外层View,其他View渲染完成后调用onRenderSuccess。
         * WXRenderStrategy.APPEND_ONCE 所有控件渲染完后后一次性返回。
         */
        Map options = new HashMap<>();
        options.put(WXSDKInstance.BUNDLE_URL, TEST_URL);
        mWXSDKInstance.renderByUrl("demoweex",TEST_URL,options,null,WXRenderStrategy.APPEND_ONCE);
    }


    @Override
    public void onViewCreated(WXSDKInstance instance, View view) {
        if (view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
        mContainer.addView(view);
    }

    @Override
    public void onRenderSuccess(WXSDKInstance instance, int width, int height) {

    }

    @Override
    public void onRefreshSuccess(WXSDKInstance instance, int width, int height) {

    }

    @Override
    public void onException(WXSDKInstance instance, String errCode, String msg) {

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityStart();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityPause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityStop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityDestroy();
        }
    }

    private void checkApiVersionAndPermission() {
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        if (currentVersion >= Build.VERSION_CODES.M) {

            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[] {Manifest.permission.INTERNET},
                        REQUEST_CODE_FOR_MODIFY_INTERNET);
            }

            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[] {Manifest.permission.ACCESS_NETWORK_STATE},
                        REQUEST_CODE_FOR_ACCESS_NETWORK_STATE);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CODE_FOR_MODIFY_INTERNET:
                if (grantResults.length <= 0
                        || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    // TODO: 16/5/26 not decide yet....request again or close the application
                }
                break;
            case REQUEST_CODE_FOR_ACCESS_NETWORK_STATE:
                if (grantResults.length <= 0
                        || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    // TODO: 16/5/26 not decide yet....request again or close the application
                }
                break;
            default:
                break;
        }
    }
}

activity_main布局文件如下




    


5.在AndroidManifest.xml中加上需要的INTERNET权限以及指定application为WXApplication


android:name=".WXApplication"

以上步骤完成,在android studio中ctrl + r,将项目调试运行,结果如下:

weex集成至android项目_第1张图片
image.png

你可能感兴趣的:(weex集成至android项目)