如何在Android程序中使用ACRA3(How to install ACRA3 in your Android application)

转载:http://blog.csdn.net/chenyafei617/article/details/6568605

一、介绍(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

<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 936.53125px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[java]</strong> <a target=_blank href="http://blog.csdn.net/chenyafei617/article/details/6568605#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a target=_blank href="http://blog.csdn.net/chenyafei617/article/details/6568605#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a><div style="position: absolute; left: 438px; top: 1247px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">package</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> com.chen.android.cr;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> org.acra.ACRA;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> org.acra.annotation.ReportsCrashes;  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.app.Application;  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@ReportsCrashes</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(formKey=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"dHNfYkVsNWdMbTdaTFlPUWJlUkg5cWc6MQ"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyApplication </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Application{  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li></ol></div> 
在 <tt>MyApplication</tt> 类中, 覆盖 onCreate() 方法并添加ACRA初始化代码
<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 936.53125px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[java]</strong> <a target=_blank href="http://blog.csdn.net/chenyafei617/article/details/6568605#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a target=_blank href="http://blog.csdn.net/chenyafei617/article/details/6568605#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a><div style="position: absolute; left: 438px; top: 1580px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//覆盖 onCreate() 方法并添加ACRA初始化代码</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onCreate() {  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    System.out.println(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"============init================="</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;"> The following line triggers the initialization of ACRA</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    ACRA.init(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onCreate();  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">};  </span></li></ol></div> 
打开android配置文件<tt>AndroidManifest.xml</tt><tt></tt>
<ul><li><span style="font-size:12px;">设置项目的Application为MyApplication</span></li><li><span style="font-size:12px;">添加权限声明<tt>android.permission.INTERNET</tt></span></li></ul>

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.chen.android.cr"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"  
  9.         android:name=".MyApplication">  
  10.         <activity android:name=".MainActivity"  
  11.                   android:label="@string/app_name">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.                 <category android:name="android.intent.category.LAUNCHER" />  
  15.             </intent-filter>  
  16.         </activity>  
  17.     </application>  
  18.       
  19.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  20. </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:
    <span class="lit" style="color: rgb(0, 102, 102);">@ReportsCrashes</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">formKey</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"dGVacG0ydVHnaNHjRjVTUTEtb3FPWGc6MQ"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln">
                                    mode </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">ReportingInteractionMode</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">TOAST</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln">
                                    resToastText </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">crash_toast_text</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln">
    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">MyApplication</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">extends</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Application</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln">
    </span><span class="pun" style="color: rgb(102, 102, 0);">...</span>

    在 strings.xml 中

    [xhtml] view plaincopy
    1. <string name="crash_toast_text">Ooooops ! I crashed, but a report has been sent to my developer to help him fix the issue !</string>  

     状态栏通知:

    [xhtml]  view plain copy
    1. @ReportsCrashes(formKey="dGVacG0ydVHnaNHjRjVTUTEtb3FPWGc6MQ",  
    2.                                 mode = ReportingInteractionMode.NOTIFICATION,  
    3.                                 resNotifTickerText = R.string.crash_notif_ticker_text,  
    4.                                 resNotifTitle = R.string.crash_notif_title,  
    5.                                 resNotifText = R.string.crash_notif_text,  
    6.                                 resNotifIcon = android.R.drawable.stat_notify_error, // optional. default is a warning sign  
    7.                                 resDialogText = R.string.crash_dialog_text,  
    8.                                 resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign  
    9.                                 resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name  
    10.                                 resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. when defined, adds a user text field input with this text resource as a label  
    11.                                 resDialogOkToast = R.string.crash_dialog_ok_toast // optional. displays a Toast message when the user accepts to send a report.  
    12.                                 )  
    13. public class MyApplication extends Application {  
    14. ...  

     

    在 strings.xml 中:

    [xhtml]  view plain copy
    1. <string name="crash_notif_ticker_text">Unexpected error, please send a report...</string>  
    2. <string name="crash_notif_title">CrashTest has crashed...</string>  
    3. <string name="crash_notif_text">Please click here to help fix the issue.</string>  
    4.           
    5. <string name="crash_dialog_title">CrashTest has crashed</string>  
    6. <string name="crash_dialog_text">An unexpected error occurred forcing the  
    7.         application to stop. Please help us fix this by sending us error data,  
    8.         all you have to do is click /'OK/'.</string>  
    9. <string name="crash_dialog_comment_prompt">You might add your comments about the problem below:</string>  
    10. <string name="crash_dialog_ok_toast">Thank you !</string>  

     

    In your AndroidManifest.xml

    [xhtml] view plaincopy
    1. <application ...>  
    2.   
    3.     ....  
    4.   
    5.     <activity android:name="org.acra.CrashReportDialog"  
    6.         android:theme="@android:style/Theme.Dialog"  
    7.         android:launchMode="singleInstance"  
    8.         android:excludeFromRecents="true"  
    9.         android:finishOnTaskLaunch="true" />  
    10.   
    11.     ....  
    12.   
    13. </application>  

     

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值