如何在ACRA自定义发件人中获得Exception或Throwable

时间:2014-05-22 11:04:13

标签: android acra

您好我需要向Airbrake发送崩溃报告但我相信ACRA在Android平台上是用于此目的的更强大的工具所以我一直在尝试将它们结合起来并为ACRA实现我自己的ReposrtSenderAirbrakeNotifier.notify();。不幸的是,它需要将Exception作为参数,但send()方法仅提供CrashReportData

public interface ReportSender {
    /**
     * Send crash report data. You don't have to take care of managing Threads,
     * just implement what is necessary to handle the data. ACRA will use a
     * specific Thread (not the UI Thread) to run your sender.
     * 
     * @param errorContent
     *            Stores key/value pairs for each report field. A report field
     *            is identified by a {@link ReportField} enum value.
     * @throws ReportSenderException
     *             If anything goes fatally wrong during the handling of crash
     *             data, you can (should) throw a {@link ReportSenderException}
     *             with a custom message.
     */

    public void send(CrashReportData errorContent) throws ReportSenderException;
}

以下是我的发送方实施,应该向Airbrake发送报告:

public class MyReportSender implements ReportSender {
    public void send(CrashReportData errorContent) throws ReportSenderException {
        AirbrakeNotifier.notify(/*Exception is required here*/);
        // But only key/value pairs are available here in errorContent param.
    }
}

我注意到我可以使用CrashReporData中的一些数据构造一个新的异常,但如果可能的话,最好能获得原始的异常。

2 个答案:

答案 0 :(得分:0)

将ACRA jar文件放在项目的libs目录中并创建CrashReport.java文件

@ReportsCrashes(formUri = "http://www.bugsense.com/api/acra?api_key=6f3855c9", formKey="")
//api_key you will get once you register your application with ACRA.

public class CrashReport extends Application {
@Override
public void onCreate() {
    super.onCreate();

    // The following line Application register with ACRA
    ACRA.init(this);
}
}

将崩溃报告类设置在menifest.xml文件中的应用程序标记内,如下所示

android:name="com.example.mydoc.CrashReport"

此处com.example.mydoc是崩溃报告所在的应用程序包。

答案 1 :(得分:0)

虽然这个问题已经过去了,但我还是想回答它,因为我通过谷歌发现它,其他人也可能会这样做。

Exception内的ReportSender是不可能的。您可以获取其堆栈跟踪,但不能获取对象本身(因为它与您的发件人位于不同的进程中,并且此时可能已经进行了垃圾回收)。

如果您需要该对象,则可以使用ReportPrimer代替。它在构建报告之前被调用,因此可以访问ReportBuilder,其中包含Exception个对象(以及其他数据,例如崩溃的Thread)。