Search code examples
androidacra

Android - How to programmatically set USER_EMAIL in Acra crash report?


My app uses ACRA to send a silent notification if the app crashes.

The official documentation shows how to set the user email in XML and this SO question shows how to set it via annotations, but how can I set the user email address in code?

Presuming I've started off in the right direction, this is as far as I've got...

ACRA.init(this);
ACRAConfiguration acraConfig = ACRA.getConfig();
//acraConfig.howToSetUserEmail???

UPDATE

Thanks to 323go's helpful answer, here is the working code:

import org.acra.*;
import org.acra.annotation.ReportsCrashes;
import android.app.Application;
import android.content.Context;

@ReportsCrashes(
    formKey = "", // This is required for backward compatibility but not used
    formUri = "http://example.com/crash-reports/my-script-which-emails-me-the-acra-data.php",
    sharedPreferencesName = "THE_NAME_OF_MY_SHARED_PREFS_FILE",
    sharedPreferencesMode = Context.MODE_PRIVATE
)
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // The following line triggers the initialization of ACRA
        ACRA.init(this);

    }

}

Then, elsewhere in my app, I set the acra.user.email preference in a similar manner as shown in the accepted answer, below.


Solution

  • The documentation says you can do it through SharedPreferences:

    getSharedPreferences().edit().putString( "acra.user.email", "[email protected]" ).commit();
    

    should do the trick. I haven't tested that, as I don't see much use for setting the email address at run-time in my setup.