简体   繁体   English

Android-如何在Acra崩溃报告中以编程方式设置USER_EMAIL?

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

My app uses ACRA to send a silent notification if the app crashes. 如果应用崩溃,我的应用将使用ACRA发送静默通知。

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? 官方文档显示了如何以XML设置用户电子邮件, 这个SO问题显示了如何通过注释设置用户电子邮件,但是如何在代码中设置用户电子邮件地址呢?

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 UPDATE

Thanks to 323go's helpful answer, here is the working code: 感谢323go的有用回答,这是工作代码:

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. 然后,在我应用程序的其他位置,我以与下面接受的答案所示类似的方式设置acra.user.email首选项。

The documentation says you can do it through SharedPreferences : 该文档说您可以通过SharedPreferences来做到这一点:

getSharedPreferences().edit().putString( "acra.user.email", "your@email.com" ).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. 我还没有测试过,因为在设置中我看不出在运行时设置电子邮件地址有太多用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM