weex sdk 安卓 android集成
程序员文章站
2022-06-23 08:50:10
weex sdk集成到安卓,可以参考官网文档,但是会报错;这里总结一下可用的方法及方式;最后提供demo下载可以直接使用,不过为helloword版本;其中weex开发网页部分,需要自行进行后续处理。官方网址:week集成manifest添加权限:
weex sdk集成到安卓,可以参考官网文档,但是会报错;这里总结一下可用的方法及方式;最后提供demo下载可以直接使用,不过为helloword版本;
其中weex开发网页部分,需要自行进行后续处理。
官方网址:week集成
- manifest
添加权限:
<!-- 网络-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- sd卡读写-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
生命application
<application
android:name=".MyApplication"
- gradle
增加阿里的镜像,这样下载更快
repositories {
google()
jcenter()
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
plugins {
id 'com.android.application'
}
apply from: 'https://www.mobibrw.com/wp-content/uploads/2019/11/download_jsc.gradle'
android {
compileSdkVersion 29
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "tv.newtv.weexdemo"
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation fileTree(dir: "libs", include: ["*.jar"])
// 以下两个 weex_sdk 版本二选一
// implementation 'org.apache.weex:sdk:0.28.0'
implementation 'org.apache.weex:sdk_legacy:0.28.0'
// fastjson
implementation 'com.alibaba:fastjson:1.1.46.android'
//support library dependencies
- application
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: .......");
InitConfig config = new InitConfig.Builder()
.setHttpAdapter(new DefaultWXHttpAdapter()).build();
WXSDKEngine.initialize(this, config);
WXBridgeManager.updateGlobalConfig("wson_on");
WXEnvironment.setOpenDebugLog(true);
WXEnvironment.setApkDebugable(true);
WXSDKEngine.addCustomOptions("appName", "WXSample");
WXSDKEngine.addCustomOptions("appGroup", "WXApp");
}
}
- mainactivity
public class MainActivity extends AppCompatActivity implements IWXRenderListener {
private static final String TAG = "MainActivity";
private WXSDKInstance wxsdkInstance;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
/**
* 轮询访问 WXSDKEngine 初始化状态 防止异步造成的初始化失败问题
*/
if (msg.what == 1) {
if (WXSDKEngine.isInitialized()) {
startRender();
} else {
handler.sendEmptyMessageDelayed(1, 300);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
wxsdkInstance = new WXSDKInstance(this);
wxsdkInstance.registerRenderListener(this);
if (WXSDKEngine.isInitialized()) {
startRender();
} else {
handler.sendEmptyMessageDelayed(1, 300);
}
setContentView(R.layout.activity_main);
}
/**
* WXSDKEngine 初始化成功后 开始渲染
*/
private void startRender() {
String pageName = "WXSample";
/**
* 防止空指针
*/
if (wxsdkInstance == null) {
wxsdkInstance = new WXSDKInstance(this);
wxsdkInstance.registerRenderListener(this);
}
/**
* 渲染远程js
*/
String bundleUrl = "http://dotwe.org/raw/dist/38e202c16bdfefbdb88a8754f975454c.bundle.wx";
wxsdkInstance.renderByUrl(pageName, bundleUrl, null, null, WXRenderStrategy.APPEND_ASYNC);
/**
* 渲染本地js
*/
// String bundleUrl = "index.js";
// wxsdkInstance.render(pageName, WXFileUtils.loadAsset(bundleUrl,this), null, null, WXRenderStrategy.APPEND_ASYNC);
}
/**
* 重写生命周期方法
*/
@Override
protected void onResume() {
super.onResume();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityResume();
}
}
@Override
protected void onPause() {
super.onPause();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityPause();
}
}
@Override
protected void onStop() {
super.onStop();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityDestroy();
}
}
@Override
public void onViewCreated(WXSDKInstance wxsdkInstance, View view) {
/**
* 填充视图
*/
setContentView(view);
Log.d(TAG, "onViewCreated: .......");
}
@Override
public void onRenderSuccess(WXSDKInstance wxsdkInstance, int i, int i1) {
}
@Override
public void onRefreshSuccess(WXSDKInstance wxsdkInstance, int i, int i1) {
}
@Override
public void onException(WXSDKInstance wxsdkInstance, String errCode, String msg) {
Log.d("welog", "onException: " + errCode + "//" + msg);
}
- demo地址
本文地址:https://blog.csdn.net/shi_xin/article/details/110952283
推荐阅读
-
安卓(Android)聊天机器人实现代码分享
-
android播放音频文件(安卓音频输出设置步骤)
-
内存修改器安卓(Android内存优化工具)
-
最新版安卓应用(android)开发环境搭建图解教程(带Andriod NDK)
-
Android 5.0上如何开启Google now?安卓5.0 Google now启用方法
-
android播放音频文件(安卓音频输出设置步骤)
-
Android集成阿里百川电商SDK踩过的坑总结
-
无线路由(MERCURY水星为例)与Android安卓手机无线连接设置指南(图文教程)
-
通过Html网页调用本地安卓(android)app程序代码
-
android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法