开发插件

编写 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.text.TextUtils;
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.ISysEventListener;
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";
private IWebview webview = null;
private String wakeupCallBackID = null; @Override
public void onStart(Context context, Bundle bundle, String[] strings) {
super.onStart(context, bundle, strings);
Log.d(TAG, "init");
OpenInstall.init(context);
} public void registerWakeUpHandler(final IWebview pWebview, JSONArray array) {
Log.d(TAG, "registerWakeUpHandler");
String callBackID = array.optString(0); webview = pWebview;
wakeupCallBackID = callBackID;
// 自己注册监听并处理 onNewIntent 事件
pWebview.obtainApp().registerSysEventListener(new ISysEventListener() {
@Override
public boolean onExecute(SysEventType sysEventType, Object o) {
if (sysEventType == SysEventType.onNewIntent) {
String dataString = (String) o;
Intent intent = new Intent();
intent.setData(Uri.parse(dataString));
if (webview != null && wakeupCallBackID != null) {
getWakeUp(intent, webview, wakeupCallBackID);
}
}
return false;
}
}, SysEventType.onNewIntent); Intent intent = pWebview.getActivity().getIntent();
if (intent == null || TextUtils.isEmpty(intent.getDataString())) {
return;
}
getWakeUp(intent, pWebview, callBackID);
} @Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 5+sdk 的 bug 导致 onNewIntent 未被回调
// if (webview != null && wakeupCallBackID != null) {
// getWakeUp(intent, webview, wakeupCallBackID);
// }
} 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.d(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.d(TAG, "reportRegister");
OpenInstall.reportRegister();
} public void reportEffectPoint(IWebview pWebview, JSONArray array) {
Log.d(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 = {
//注册拉起回调
registerWakeUpHandler: function (successCallback) {
var success = typeof successCallback !== 'function' ? null : function(args) {
successCallback(args);
},
callbackID = B.callbackId(success, null);
return B.exec(_BARCODE, "registerWakeUpHandler", [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">
<!-- openinstall 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:launchMode="singleTask"
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" src="./js/openinstall.js"></script>
获取 scheme 唤醒数据

在应用启动时,注册唤醒回调。这样当 App 被唤醒时,会回调传入的方法,并在回调中获取唤醒数据

document.addEventListener('plusready',function(){
plus.openinstall.registerWakeUpHandler(function(data){
console.log("wakeup : channelCode= "
+ data.channelCode + ", bindData=" + data.bindData);
alert("wakeup : channelCode= " + data.channelCode + ", bindData=" + data.bindData);
}); },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);
}

**openinstall 官方已提供 hbuilder 集成插件 [openinstall-hbuilder-sdk](https://github.com/OpenInstall/openinstall-hbuilder-sdk),包含了iOS和Android两个平台**

HBuilder 插件开发(openinstall 集成)的更多相关文章

  1. openinstall集成小技巧

    引言:最近在做一个iOS端的小游戏,想要实现在安装时自动关联好友的功能,就发帖询问有没有好的想法.在帖子中法想了这个不错的SDK,通过它我们还实现了,安装后自动进入好友游戏房间的功能.这里我就分享一下 ...

  2. 记录vue项目 用hbuilder离线打包集成极光推送 安卓篇

    极光推送的官方demo: https://github.com/jpush/jpush-hbuilder-demo 里面也记录有详细的方法了. 我记录下自己的过程. 首先去极光那里创建一个应用 获取A ...

  3. Mac 下sublime的插件

    最近更换了本本,入了港行的Mac pro,来替代原来的Thinkpad,在windows上工作做完之后,搭建了一下开发环境,eclipse是必须的,但是luna没有html editor,然后就在 e ...

  4. More development resources

    社区 名称 官网 google https://www.google.com/ github https://github.com/ StackOverflow https://stackoverfl ...

  5. Hudson(Jenkins)持续集成插件开发环境搭建

    Hudson持续集成插件开发环境搭建 第一步安装java jdk,至于版本的话推荐1.6以上吧.安装好jdk设置环境变量,确保你在cmd中输入java -version有提示你jdk的版本信息等,也就 ...

  6. Cocos2d-x 集成openinstall(Android)

    目的: 1. Cocos2d-x集成openinstall sdk? 有这么一个场景,甲给乙分享了链接,乙使用并下载APP,推荐者甲和乙的关系这个思路是怎样的? 你首先想到的也许会说,那当然就是给对方 ...

  7. unity集成openinstall流程

    目的 1.Unity集成openinstall sdk? 最近在使用一个叫openinstall的SDK,通过它实现免填邀请码的功能,集成到unity游戏开发中.对App安装流程的优化,尤其是免填写邀 ...

  8. HBuilder的扩展插件开发暴露了一个事实:其实不能实现写一次代码实现跨平台App生成

    HBuilder的扩展插件开发,原来并不能生成单独的插件jar,而是以源码 - 类的形式进行开发,这其实就要求必须使用离线打包. 事实上,开发顺序应该是:先弄好离线打包框架,然后在AS里进行扩展插件开 ...

  9. unity插件开发——一个例子:简单的svn集成

    在unity开发过程中,通常我们习惯性地在Windows操作系统下使用svn进行版本管理,而每次提交更新,都需要回到文件夹下的这种操作让人无法忍受.是不是可以集成svn到unity中呢?查了一圈uni ...

随机推荐

  1. Android开发概要记录

    1..o文件. .ko和.so文件的路径 \kernel\out\mediatek---------------.o文件 .c/.cpp文件编译生成的目标文件 \out\target\product\ ...

  2. win7 64位系统,vs2010下配置OpenGL开发环境

    glut下载地址: http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip 或者:http://user.xmission. ...

  3. Loader转换器

    一.简介 webpack本身只能处理js模块,Loader可以理解为模块和资源的转换器,它本身是一个函数,接受文件作为参数,返回转换的结果.因此,我们就能通过require来加载任何类型的模块和文件. ...

  4. 简单了解JS中的几种遍历

    忙了好一段时间,项目上线后终于有那么一点点空档期静下来整理一些问题了.当我们在开发项目的时候,用到遍历的地方肯定少不了,那么我们有那么多的遍历方法,在不同情况下用那种方法会更优雅而且还没bug呢? 首 ...

  5. 关于减少BUG的思考

    开发之前,就要先设计,理清好思路:如果需求都不清楚,软件肯定有缺陷: 和客户.测试的沟通 如果开发出来的东西本身就有BUG,交给测试,会浪费很多人的时间. 开发做完一个功能后,要自己做一遍测试 自己的 ...

  6. Struts2,Spring,Hibernate优缺点

    struts框架具有组件的模块化,灵活性和重用性的优点,同时简化了基于MVC的web应用程序的开发. 优点: Struts跟Tomcat.Turbine等诸多Apache项目一样,是开源软件,这是它的 ...

  7. java并发包分析之———AQS框架

    一.什么是同步器   多线程并发的执行,之间通过某种 共享 状态来同步,只有当状态满足 xxxx 条件,才能触发线程执行 xxxx . 这个共同的语义可以称之为同步器.可以认为以上所有的锁机制都可以基 ...

  8. XML 和 java对象相互转换

    XML 和 java对象相互转换 博客分类: XML 和 JSON   下面使用的是JDK自带的类,没有引用任何第三方jar包. Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 ...

  9. 学习MACD指标

    概念 MACD叫指数平滑异同移动平均线指标. 零轴 MACD柱线 DIFF线 DEA线 使用 一般出现如下情形,股价处于或即将进入上涨趋势中: MACD指标在零轴上方出现金叉,其后DIFF快线一直位于 ...

  10. Linux 库函数与系统调用的关系与区别

    上周总结了<C 标准库的基础 IO>,其实这些功能函数通过「系统调用」也能实现相应功能.这次文章并不是要详细介绍各系统调用接口的使用方法,而是要深入理解「库函数」与「系统」调用之间的关系和 ...