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()但没有运气。

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

在最新版本的 ACRA (4.5.0) 中,您需要调用 ACRA#init(),然后调用Thread#setDefaultUncaughtExceptionHandler()提供您自己的处理程序来调用 ErrorReport#setCustomData() U,然后调用您要替换的UncaughtExceptionHandler(这将是 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);
    }
  }
);

如果您想从主服务器中剪切ACRA版本,则可以利用 https://github.com/ACRA/acra/pull/87 并能够像以下方式配置ACRA:

ACRA.init(this);
ACRA.getErrorReporter().setExceptionHandlerInitializer(new ExceptionHandlerInitializer() {
    void initializeExceptionHandler(ErrorReporter reporter) {
      ACRA.getErrorReport().putCustomData("foo", "bar");
    }
  }
);

感谢您帮助我,我解决了我的问题并满足了我需要的东西,我只是从 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...
    }
});

使用以下类 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

        }
    }
}

相关内容

  • 没有找到相关文章

最新更新