Ionic2中集成腾讯Bugly之自定义插件

Ionic2混合开发,入坑系列:Ionic2中集成腾讯Bugly之自定义插件

 

1、编写Bugly.js代码
var exec = require('cordova/exec');
module.exports = {
    /**
     * 发送Bugly异常信息
     * @param success 成功回调函数
     * @param error   错误回调函数
     * @param params  参数值
     */
    buglySend: function (success, error, params) {
        exec(success, error, "CallCordovaBugly", "buglySend", [params]);
    }
};
2、配置plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Bugly自定义插件配置-->

<plugin xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:rim="http://www.blackberry.com/ns/widgets"
    id="cordova-plugin-bugly" version="1.0.0"
    xmlns="http://apache.org/cordova/ns/plugins/1.0">
    <name>Bugly</name>
    <description>Cordova Bugly Plugin</description>
    <license>Apache 2.0</license>
    <keywords>cordova,bugly</keywords>
    <engines>
        <engine name="cordova" version=">=3.0.0" />
    </engines>
    <js-module name="bugly" src="www/bugly.js">
        <clobbers target="window.plugins.bugly" />
    </js-module>
</plugin>
3、编写TypeScript代码
import { Plugin, Cordova } from 'ionic-native'

@Plugin({
        pluginName: 'Bugly',
        plugin: 'cordova-plugin-bugly',
        pluginRef: 'window.plugins.bugly',
        platforms: ['Android', 'iOS']
        })
export class Bugly {
    @Cordova({ promise: true })
    static buglySend(params: string): Promise<string> { return; }
}
4、配置config.xml文件
<feature name="CallCordovaBugly">
    <param name="android-package" value="org.apache.cordova.bugly.CallCordovaBugly" />
</feature>
5、编写自定义插件类(Java代码)
package org.apache.cordova.bugly;
import android.util.Log;
import com.tencent.bugly.crashreport.CrashReport;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * //TODO (用一句话描述该文件做什么)
 *
 * @package: org.apache.cordova.bugly.CallCordovaBugly
 * @author: ailsabe@126.com
 * @date: 2017/1/20
 * @description: //TODO (详细描述该文件做什么)
 */
public class CallCordovaBugly extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        try {
            if (args.length() > 0) {
                Log.i("Bugly", args.toString());
                //RuntimeException e = new RuntimeException("自定义异常上报");
                throw new RuntimeException(args.toString());
            }

        } catch (Exception e) {
            CrashReport.postCatchedException(e);
        }
        return true;
    }
}
6、在Application中初始化Bugly环境
Bugly有自动上报与手动上报区分
//初始化Bugly收集器(为了保证运营数据的准确性,建议不要在异步线程初始化Bugly)
//CrashReport.initCrashReport(getApplicationContext(),"VvqUAywlen5oT4IH",false);
CrashReport.initCrashReport(getApplicationContext()); 

 

 
posted @ 2017-03-01 16:34  fujc2dev  阅读(1508)  评论(0编辑  收藏  举报