HBuilder第三方插件開(kāi)發(fā)(Android平臺(tái)openinstall集成)
HBuilder第三方插件开发(Android平台openinstall集成)
离线打包
如果要集成使用非基座包下的第三方 SDK,就必须使用离线打包。可以参考 官方文档 进行离线打包,如果官方文档太难懂,可以查看 其他技术人员的教程
开发插件
编写 Android 原生代码
下载 openinstall SDK 并将 jar 包拷贝到项目的 libs 目录。创建一个 package,如 com.wenkiwu.hbuilder.openinstall;在包中新建一个类继承自 StandardFeature,然后对应openinstall的接口定义相应的功能方法。完整代码如下:
package com.wenkiwu.hbuilder.openinstall;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.util.Log;import com.fm.openinstall.OpenInstall;import com.fm.openinstall.listener.AppInstallAdapter;import com.fm.openinstall.listener.AppWakeUpAdapter;import com.fm.openinstall.model.AppData;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import io.dcloud.common.DHInterface.IWebview;import io.dcloud.common.DHInterface.StandardFeature;import io.dcloud.common.util.JSUtil;public class OpenInstallApiManager extends StandardFeature { private static final String TAG = "OpenInstallApiManager"; @Override
public void onStart(Context context, Bundle bundle, String[] strings) { super.onStart(context, bundle, strings);
Log.w(TAG, "init");
OpenInstall.init(context);
} public void getWakeUp(final IWebview pWebview, JSONArray array) {
Log.w(TAG, "getWakeUp"); final String callBackID = array.optString(0);
String url = array.optString(1);
Intent intent = new Intent();
intent.setData(Uri.parse(url)); getWakeUp(intent, pWebview, callBackID);
} private void getWakeUp(Intent intent, final IWebview pWebview, final String callBackID) {
OpenInstall.getWakeUp(intent, new AppWakeUpAdapter() { @Override
public void onWakeUp(AppData appData) {
JSONObject dataJson = new JSONObject(); try {
dataJson.put("channelCode", appData.getChannel());
dataJson.put("bindData", appData.getData());
} catch (JSONException e) {
e.printStackTrace();
}
JSUtil.execCallback(pWebview, callBackID, dataJson, JSUtil.OK, false);
}
});
} public void getInstall(final IWebview pWebview, JSONArray array) {
Log.w(TAG, "getInstall"); final String callBackID = array.optString(0); int timeout = -1; if (array.isNull(1)) {
timeout = array.optInt(1);
}
OpenInstall.getInstall(new AppInstallAdapter() { @Override
public void onInstall(AppData appData) {
JSONObject dataJson = new JSONObject(); try {
dataJson.put("channelCode", appData.getChannel());
dataJson.put("bindData", appData.getData());
} catch (JSONException e) {
e.printStackTrace();
}
JSUtil.execCallback(pWebview, callBackID, dataJson, JSUtil.OK, false);
}
}, timeout * 1000);
} public void reportRegister(IWebview pWebview, JSONArray array) {
Log.w(TAG, "reportRegister");
OpenInstall.reportRegister();
} public void reportEffectPoint(IWebview pWebview, JSONArray array) {
Log.w(TAG, "reportEffectPoint");
String pointId = array.optString(0); long pointValue = array.optLong(1);
OpenInstall.reportEffectPoint(pointId, pointValue);
}
}封装插件的 JS
在前端代码的 js 文件夹中,新建 openinstall.js,编写代码,通过 plus.bridge 调用 Native 层的方法
document.addEventListener( "plusready", function(){
var _BARCODE = 'openinstall',
B = window.plus.bridge;
var openinstall = {
//获取拉起携带数据
getWakeUp: function (successCallback) {
var success = typeof successCallback !== 'function' ? null : function(args) {
successCallback(args);
},
callbackID = B.callbackId(success, null);
return B.exec(_BARCODE, "getWakeUp", [callbackID]);
},
// 获取安装来源数据
getInstall : function (successCallback, timeout) {
var success = typeof successCallback !== 'function' ? null : function(args) {
successCallback(args);
},
callbackID = B.callbackId(success, null);
return B.exec(_BARCODE, "getInstall", [callbackID, timeout]);
},
// 注册上报
reportRegister : function () {
return B.exec(_BARCODE, "reportRegister", []);
},
// 上报渠道效果
reportEffectPoint : function (pointId, pointValue) {
return B.exec(_BARCODE, "reportEffectPoint", [pointId, pointValue]);
}
};
window.plus.openinstall = openinstall;}, true );集成插件
关联 JS 插件名和 Android 原生类
修改项目的 src/main/assets/data/ 目录下的 dcloud_properties.xml文件,指定 JS 对象名称和 Android 的类名对应关系,以便 H5+ SDK 根据对应的 JS 名查找并生成相应的 Native 对象执行对应的逻辑
<properties> <features> <!-- more feature --> <!-- openinstall plugin --> <feature name="openinstall" value="com.wenkiwu.hbuilder.openinstall.OpenInstallApiManager"/> </features> <services> <!-- openinstall需要在程序启动时初始化 --> <service name="openinstall" value="com.wenkiwu.hbuilder.openinstall.OpenInstallApiManager"/> <!-- more service --> </services></properties>
在应用的 manifest.json 文件中还需要添加扩展插件的应用使用权限
{
"@platforms": [
"android",
"iPhone",
"iPad"
],
"id": "H5E1BA598",
"name": "OpenInstallPlugin",
// ...
"permissions": {
"Console": {
"description": "跟踪调试输出日志"
},
"Events": {
"description": "应用扩展事件"
},
// openinstall plugin
"openinstall": {
"description": "openinstall插件"
}
},
// ...}openinstall 的配置
根据openinstall官方文档,在 AndroidManifest.xml 中做以下配置
声明权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.INTERNET"/>
配置 AppKey 和 scheme
<application android:allowBackup="false" android:allowClearUserData="true" android:icon="@drawable/icon" android:label="@string/app_name" android:largeHeap="true" android:supportsRtl="true"> <!-- openiinstall appkey 配置 --> <meta-data android:name="com.openinstall.APP_KEY" android:value="OPENINSTALL_APPKEY"/> <activity android:name="io.dcloud.PandoraEntry" android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale" android:hardwareAccelerated="true" android:screenOrientation="user" android:theme="@style/TranslucentTheme" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <!-- opeinstall scheme 配置 --> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="OPENINSTALL_SCHEME"/> </intent-filter> </activity></application>
网页中 JS 调用示例
引入 JS 文件
<script type="text/javascript" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="./js/openinstall.js"></script>
获取 scheme 拉起参数
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
100積分直接送
付費(fèi)專(zhuān)欄免費(fèi)學(xué)
大額優(yōu)惠券免費(fèi)領(lǐng)