ACRA:自定义消息?

时间:2014-12-23 12:58:04

标签: android acra

我正在处理我的申请和ACRA。我想知道崩溃发生时我的应用程序的一些状态行为。怎么做?收集报告内容之前是否有任何事件监听器,以便我可以实现它并放入最新的自定义数据?

到目前为止,这是我的代码:

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    ACRA.init(this);

    boolean wifiEnable = CheckWifi();
    ACRA.getErrorReporter().putCustomData("wifiEnable", "Wifi Enable " + wifiEnable); 
} 
...

上面的代码并不符合我的要求,自定义数据wifiEnable在报告发送时甚至在发生崩溃时wifiEnable状态更改时都没有获得更新。

我尝试使用类似这样的新代码并正常工作,但只有当报告立即发送时,对于待处理报告,wifiEnable变量将替换为新状态:

ACRA.init(this);

ACRA.getErrorReporter().setReportSender(new HttpSender(
        org.acra.sender.HttpSender.Method.PUT,
        org.acra.sender.HttpSender.Type.JSON,
        null
        ) {

    @Override
    public void send(CrashReportData report) throws ReportSenderException {

        boolean wifiEnable = CheckWifi();

        report.put(ReportField.CUSTOM_DATA, "wifiEnable=" + "Wifi Enable " + wifiEnable);

        super.send(report);
    }
});

我尝试在每个活动ACRA.getErrorReporter().putCustomData()中创建onCreate(),但没有运气。

任何帮助都会很好。感谢。

3 个答案:

答案 0 :(得分:0)

使用以下类MyApplication.java并在manifest中将其定义为

 <application
        android:name="com.android.example.acra.MyApplication" />

并在MyApplication.java

package com.android.example.acra;

import org.acra.ACRA;
import org.acra.annotation.ReportsCrashes;
import org.acra.collector.CrashReportData;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;
import org.acra.util.JSONReportBuilder.JSONReportException;

import android.app.Application;

@ReportsCrashes(formKey = "", logcatArguments = { "-t", "100", "-v", "long", "ActivityManager:I",
        "MyApp:D", "*:S" })
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        // The following line triggers the initialization of ACRA
        ACRA.init(this);
        MyACRAReportSender myACRAReportSender = new MyACRAReportSender();
        ACRA.getErrorReporter().setReportSender(myACRAReportSender);

        super.onCreate();
    }

    public class MyACRAReportSender implements ReportSender {

        @Override
        public void send(CrashReportData report) throws ReportSenderException {
            String error = "";

            try {
                error = report.toJSON().toString();

            } catch (JSONReportException e) {
                error = "JSONReportException";
            }

            //TODO: Email the error or store in sdcard


        }

    }
}

答案 1 :(得分:0)

在最新版本的ACRA(4.5.0)中,您需要调用ACRA#init(),然后调用Thread#setDefaultUncaughtExceptionHandler()提供调用ErrorReport#setCustomData()的自己的处理程序然后调用{{ 1}}你要替换(这将是ACRA的)。即

UncaughtExceptionHandler

如果你想从master中删除一个版本的ACRA,那么你可以利用https://github.com/ACRA/acra/pull/87并且可以像以下一样配置ACRA:

ACRA.init(this);
final UncaughtExceptionHandler handler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
    void uncaughtException(Thread thread, Throwable throwable) {
      ACRA.getErrorReport().putCustomData("foo", "bar");
      handler.uncaughtException(thread, throwable);
    }
  }
);

答案 2 :(得分:0)

感谢您帮助我,我解决了我的问题并满足了我的需求,我只是从ACRA 4.5.0改变了来源src/main/java/org/acra/ErrorReporter.java

这是我所做的代码:

private static BeforeReportSendListener mBeforeReportSendListener;

public void setBeforeReportSendListener(BeforeReportSendListener listener) {
    mBeforeReportSendListener = listener;
}

public static interface BeforeReportSendListener {
    void onBeforeReportSend();
}

接下来,在handleException()方法中添加此代码:

if(mBeforeReportSendListener != null) {
    mBeforeReportSendListener.onBeforeReportSend();
}

这是显示我如何使用它:

ACRA.init(this);

ACRA.getErrorReporter().setBeforeReportSendListener(new ErrorReporter.BeforeReportSendListener() {

    @Override
    public void onBeforeReportSend() {
        // Check some state when crash happen
        boolean wifiEnable = CheckWifi();
        ACRA.getErrorReporter().putCustomData("wifiEnable", "Wifi Enable " + wifiEnable);
        // and more...
    }
});