ACRA: Put custom message?

1.6k Views Asked by At

I'm working with my application and ACRA. I want to know some state behavior with my application when crash occur. How it can be done? Is any event listener just before collect report content so i can implement it and put latest custom data?

This is my code so far:

public class MyApplication extends Application {

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

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

The code above doesn't meet what I want, custom data wifiEnable not getting update when report send even on wifiEnable state change at crash occur.

I try new code something like this and working, but only if report send immediately, for pending report the wifiEnable variable will replace with new state:

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);
    }
});

I try to create ACRA.getErrorReporter().putCustomData() in every activity onCreate() but no luck.

Any help would be nice. Thanks.

3

There are 3 best solutions below

1
On BEST ANSWER

Thanks for helping me, i was solve my problem and meet what i need, i just change the source src/main/java/org/acra/ErrorReporter.java from ACRA 4.5.0

This the code that i have done:

private static BeforeReportSendListener mBeforeReportSendListener;

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

public static interface BeforeReportSendListener {
    void onBeforeReportSend();
}

Next, add this code in handleException() method:

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

This is show how can i use it:

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...
    }
});
0
On

Use the below class MyApplication.java and define this in manifest as

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

and in 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
On

In the latest release version of ACRA (4.5.0) you would need to call ACRA#init() and then call Thread#setDefaultUncaughtExceptionHandler() providing your own handler that calls ErrorReport#setCustomData() AND then calls the UncaughtExceptionHandler that you are replacing (which will be ACRA's). ie

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);
    }
  }
);

If you want to cut a version of ACRA from master then you can take advantage of https://github.com/ACRA/acra/pull/87 and will be able to configure ACRA like:

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