hbuilder android插件开发,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 对象执行对应的逻辑

在应用的 manifest.json 文件中还需要添加扩展插件的应用使用权限{

"@platforms": [

"android",

"iPhone",

"iPad"

],

"id": "H5E1BA598",

"name": "OpenInstallPlugin",

// ...

"permissions": {

"Console": {

"description": "跟踪调试输出日志"

},

"Events": {

"description": "应用扩展事件"

},

// openinstall plugin

"openinstall": {

"description": "openinstall插件"

}

},

// ...}

openinstall 的配置

根据openinstall官方文档,在 AndroidManifest.xml 中做以下配置

声明权限

配置 AppKey 和 scheme

网页中 JS 调用示例

引入 JS 文件

获取 scheme 拉起参数

在应用启动和后台唤醒时,调用参数获取方法,参考dcloud文档document.addEventListener('plusready',function(){

getWakeUp();},false);function getWakeUp(){

var args= plus.runtime.arguments;

if(args){

plus.openinstall.getWakeUp(function(data){

console.log("getWakeUp : channelCode= "

+ data.channelCode + ", bindData=" + data.bindData);

}, args);

}}// 处理从后台恢复document.addEventListener('newintent',function(){

getWakeUp();},false);

获取安装参数

在需要获取来源数据时,调用以下代码,在回调中获取参数function getInstall(){

plus.openinstall.getInstall(function(data){

console.log("getInstall : channelCode= "

+ data.channelCode + ", bindData=" + data.bindData);

}, 8);}

其他统计代码

用户注册成功后,调用以下代码,上报注册统计function reportRegister(){

plus.openinstall.reportRegister();}

统计终端用户对某些特殊业务的使用效果,如充值金额,分享次数等等,调用以下代码function reportEffectPoint(){

plus.openinstall.reportEffectPoint("effect_test", 1);}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值