一、介绍(Introduction)

ACRA 允许你的Android应用将崩溃报告以谷歌文档电子表的形式进行发送。本教程将引导应用程序项目中安装ACRA

ACRA allows your Android application to send Crash Reports in a Google Docs spreadsheet. This tutorial will guide you in installing ACRA in your application project.

二、设置好你的项目(Setting-up your project)

按照以下步骤在现有的应用程序项目中安装ACRA库

Step by step installation of the ACRA library in an existing application project:

  • 下载acra库( http://acra.googlecode.com/files/acra-3.1.2.zip)并打开压缩包
  • 登录到您的谷歌文档帐户
  • 导入压缩包中的 CrashReports-template.csv (acra-3.1.2/CrashReport/doc)
  • 打开导入的文档
  • 按照自己的喜好重命名
  • 在菜单上,单击窗体/创建表单
  • 为了启动“保存”按钮,请添加描述信息
    • 如果您使用谷歌应用服务的私人领域,一定要取消选择"Require yourdomain.com sign-in to view this form."
  • 保存表单
  • 复制在表单创建页面底部的链接中的formkey
  • 打开eclipse项目
  • 创建一个 lib 目录
  • 在lib目录中添加 acra-3.1.2.jar
  • 右击 jar文件,并且添加到build path
  • 在package的root目录创建一个新的类
    • 继承android.app.Application,并命名如: MyApplication
    • 在MyApplication类声明之上,添加annotation@ReportsCrashes,并指定谷歌文档的formkey

package com.chen.android.cr;import org.acra.ACRA;import org.acra.annotation.ReportsCrashes;import android.app.Application;@ReportsCrashes(formKey="dHNfYkVsNWdMbTdaTFlPUWJlUkg5cWc6MQ")public class MyApplication extends Application{} 
在 MyApplication 类中, 覆盖 onCreate() 方法并添加ACRA初始化代码
//覆盖 onCreate() 方法并添加ACRA初始化代码public void onCreate() {System.out.println("============init=================");//// The following line triggers the initialization of ACRAACRA.init(this);super.onCreate();}; 
打开android配置文件AndroidManifest.xml
        
  • 设置项目的Application为MyApplication
  • 添加权限声明android.permission.INTERNET

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chen.android.cr" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".MyApplication"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>

  • 结束THE END - 下次应用崩溃的时候,ACRA会将崩溃报告添加到谷歌文档电子表中.
  • 建议 :可以在谷歌文档电子表格的preferences页面,设置通知规则,那么当有报告发送的时候,就会受到邮件通知了!

    三、高级用法(Advanced Usage

    用户通知

    默认情况,ACRA仅仅是将崩溃报告发送到服务端。从使用应用的用户来看,应用崩溃的时候仅仅是"Force Close"对话框是不够的。

    作为开发者,你可能更喜欢通知用户崩溃报告已经发送了...... 为什么不允许他描述一下崩溃之时他正在所操作的动作呢?

    ACRA 提供了这些选项,并且允许定制崩溃报告通知。

    有两种通知模式:

    • display a simple Toast with your developer designed text

    • display a status bar notification, then offering the user a dialog asking him to send the report or not, with an optional comment field you can chose to add or not.

    Enabling user notification only requires you to add parameters to the @ReportsCrashes annotation :

    • Toast notification:
    @ReportsCrashes(formKey="dGVacG0ydVHnaNHjRjVTUTEtb3FPWGc6MQ",
    mode
    = ReportingInteractionMode.TOAST,
    resToastText
    = R.string.crash_toast_text)
    public class MyApplication extends Application {
    ...

    在 strings.xml 中

    <string name="crash_toast_text">Ooooops ! I crashed, but a report has been sent to my developer to help him fix the issue !</string>

    状态栏通知:

    @ReportsCrashes(formKey="dGVacG0ydVHnaNHjRjVTUTEtb3FPWGc6MQ", mode = ReportingInteractionMode.NOTIFICATION, resNotifTickerText = R.string.crash_notif_ticker_text, resNotifTitle = R.string.crash_notif_title, resNotifText = R.string.crash_notif_text, resNotifIcon = android.R.drawable.stat_notify_error, // optional. default is a warning sign resDialogText = R.string.crash_dialog_text, resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. when defined, adds a user text field input with this text resource as a label resDialogOkToast = R.string.crash_dialog_ok_toast // optional. displays a Toast message when the user accepts to send a report. ) public class MyApplication extends Application { ...

    在 strings.xml 中:

    <string name="crash_notif_ticker_text">Unexpected error, please send a report...</string> <string name="crash_notif_title">CrashTest has crashed...</string> <string name="crash_notif_text">Please click here to help fix the issue.</string> <string name="crash_dialog_title">CrashTest has crashed</string> <string name="crash_dialog_text">An unexpected error occurred forcing the application to stop. Please help us fix this by sending us error data, all you have to do is click /'OK/'.</string> <string name="crash_dialog_comment_prompt">You might add your comments about the problem below:</string> <string name="crash_dialog_ok_toast">Thank you !</string>

    In yourAndroidManifest.xml

    <application ...> .... <activity android:name="org.acra.CrashReportDialog" android:theme="@android:style/Theme.Dialog" android:launchMode="singleInstance" android:excludeFromRecents="true" android:finishOnTaskLaunch="true" /> .... </application>

    详细内容请访问:http://code.google.com/p/acra/wiki/ACRA3HowTo

    更多相关文章

    1. Android开发去除标题栏title
    2. Android(安卓)自定义类库打包jar
    3. Android(安卓)启动界面 点击按钮跳转和3秒跳转
    4. Android(安卓)MediaPlayer支持的音视频格式和协议
    5. Android(安卓)6.0中添加硬件抽象层(HAL)
    6. Android(安卓)混合开发环境搭建
    7. Android音频处理学习之MediaExtractor获取aac文件后添加ADTS头
    8. android与js之间的交互
    9. Android:解决Camera.open()运行时异常RuntimeException

    随机推荐

    1. C 语言中 fun 函数怎么用?
    2. c语言计算阶乘累加和
    3. c++ switch用法
    4. C语言中 return 的作用
    5. c语言实现输出5个数就自动换行
    6. getchar在c语言中是什么意思
    7. c语言自定义函数
    8. Microsoft Visual C++ 怎么使用?
    9. c语言怎么比较两个数的大小
    10. C 语言和 C++ 有什么区别