前言
ACRA 是 Application Crash Report for Android 的缩写,看其单词知其意安卓应用程序崩溃报告。 作为安卓应用程序开发者,相信你一定很关注自己的软件在用户实际应用过程中的用户体验、系统稳定性等信息,特别是当系统出现崩溃或者运行不正常的情况下,第一手的崩溃异常信息对提升软件的整体竞争力是相当重要的。而ACRA可以很好的帮助你得到这些宝贵的信息。
项目实践
准备:
0. Android开发环境(Eclipse + ADT + AndroidSDK)
1. 下载最新版本的ACRA包 - v4.3.0
2. 从官网阅读相关文档,设置好相关环境
说明:
ACRA 允许你的安卓应用程序通过谷歌电子表格文档(Google Docs spreadsheet),邮件(email),服务端 HTTP POST 脚本(server-side HTTP POST script)等方式来处理崩溃报告。常见的可以使用邮件和服务端 HTTP POST 脚本,以下例子我将首先使用邮件的方式发送崩溃报告,之后大概介绍下如何使用服务端 HTTP POST 脚本的方式发送崩溃报告。
项目中使用到的相关文件
ACRATest
|- src
|- com.acra.mobile
|- IOMobile.java
|- IOReportSender.java
|- AndroidManifest.xml
首先看下IOMobile.java
2 formKey = "",
3 mailTo = "reports@yourdomain.com",
4 customReportContent = {
5 ReportField.APP_VERSION_NAME, ReportField.APP_VERSION_CODE,
6 ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL,
7 ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT },
8 mode = ReportingInteractionMode.TOAST,
9 forceCloseDialogAfterToast = false,
10 resToastText = R.string.crash_error_report_toast_text)
11 public class IOMobile extends Application {
12
13 @Override
14 public void onCreate() {
15 ACRA.init(this);
16 ErrorReporter.getInstance().setReportSender(new IOReportSender(this));
17 super.onCreate();
18 }
19
20 }
1. IOMobile 继承 Application
2. IOMobile 在 AndroidManifest.xml 文件中的配置要正确
3. 第15行代码 ACRA.init(this),将ACRA注入到项目中
4. 第16行代码 new IOReportSender(this),用于定制自己的ReportSender
再来看看 IOReportSender.java
private static final String TAG = "IOReportSender";
private Context context = null;
public IOReportSender(Context context) {
this.context = context;
}
@Override
public void send(CrashReportData errorContent) throws ReportSenderException {
sendMailReport(errorContent);
}
private void sendMailReport(CrashReportData errorContent) throws ReportSenderException {
new EmailIntentSender(context).send(errorContent);
}
}