閱讀619 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android ACRA3介紹

https://www.cnblogs.com/xiongbo/archive/2011/06/01/2063487.html

一、介紹(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庫( https://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 

Java代碼  收藏代碼
  1. package com.chen.android.cr;  
  2.   
  3. import org.acra.ACRA;  
  4. import org.acra.annotation.ReportsCrashes;  
  5.   
  6. import android.app.Application;  
  7.   
  8. @ReportsCrashes(formKey="dHNfYkVsNWdMbTdaTFlPUWJlUkg5cWc6MQ")  
  9. public class MyApplication extends Application{  
  10.   
  11. }  

在 MyApplication 類中, 覆蓋 onCreate() 方法並添加ACRA初始化代碼 

Java代碼  收藏代碼
  1. //覆蓋 onCreate() 方法並添加ACRA初始化代碼  
  2. public void onCreate() {  
  3.     System.out.println("============init=================");  
  4.     //// The following line triggers the initialization of ACRA  
  5.     ACRA.init(this);  
  6.     super.onCreate();  
  7. };  

打開android配置文件AndroidManifest.xml 
設置項目的Application為MyApplication 
添加權限聲明android.permission.INTERNET 
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="https://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頁麵,設置通知規則,那麼當有報告發送的時候,就會受到郵件通知了!

高級用法

用戶通知

默認情況,ACRA僅僅是將崩潰報告發送到服務端。從使用應用的用戶來看,應用崩潰的時候僅僅是"Force Close"對話框是不夠的。

作為開發者,你可能更喜歡通知用戶崩潰報告已經發送了...... 為什麼不允許他描述一下崩潰之時他正在所操作的動作呢?

ACRA 提供了這些選項,並且允許定製崩潰報告通知。

有兩種通知模式:

  • 顯示定製內容的Toast
  • 顯示一個狀態欄通知,然後顯示一個對話框詢問用戶是否將崩潰報告發送到服務端,並且允許用戶增加補充說明.
  

開啟用戶通知僅僅需要給@ReportsCrashes添加幾個參數 :

  • Toast 通知:
@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>

在 AndroidManifest.xml中

<application ...>

    ....

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

    ....

</application>

我可以將崩潰報告發送到自定義的服務端麼?

僅僅需要給@ReportsCrashes指定 formUri 參數 :

@ReportsCrashes(formKey="", // will not be used
                                formUri
="https://yourserver.com/yourscript",
                                mode
= ReportingInteractionMode.TOAST,
                                resToastText
= R.string.crash_toast_text)
public class MyApplication extends Application {
...

然後你的腳本必須按照 ErrorReporter 類指定字段.

我可以在崩潰報告中添加自定義的補充信息字段麼 ?

當然!

僅僅需要使用一下的方法:

ErrorReporter.getInstance().putCustomData("myVariable", myVariable);

所有定製的數據 (only latest value for each one) 將被添加到"custom"字段

可以使用 getCustomData("myVariable") 和 removeCustomData("myVariable") 來增加或刪除定製數據.

可以讓用戶禁用報告麼?

是的!

你所需要做的是添加給你的 preferences 文件添加一個CheckBoxPreference :

<CheckBoxPreference android:key="acra.disable"
       
android:title="@string/pref_disable_acra"
       
android:summaryOn="@string/pref_acra_disabled"
       
android:summaryOff="@string/pref_acra_enabled"
       
android:defaultValue="false"/>

如果你更喜歡用戶允許默認選擇發送報告,你可以按照如下配置:

<CheckBoxPreference android:key="acra.enable"
       
android:title="@string/pref_disable_acra"
       
android:summaryOn="@string/pref_acra_enabled"
       
android:summaryOff="@string/pref_acra_disabled"
       
android:defaultValue="true"/>

然後在你的 strings.xml 文件中添加3個string資源

我可以將異常也發送報告麼?或者當應用處於特殊狀態時也發送一份報告麼?

作為一個優秀的程序員,你的代碼充滿了try / catch語句,有時意想不到的異常發生了,這是你可能很想獲得當時的異常報告。

你也可能當應用出在某種狀態的時候,發送一份報告給開發人員。

你隻要按照以下方法就可以實現了 :

ErrorReporter.getInstance().handleException(caughtException);

你可以捕獲或提供任何自定義異常

如果你需要後台發送報告,而不管當前設置的是什麼通知模式,你隻需要按照以下方法即可:

ErrorReporter.getInstance().handleSilentException(caughtException);

https://code.google.com/p/acra/wiki/ACRA3HowTo?tm=6

最後更新:2017-04-02 17:09:26

  上一篇:go 開源異步處項目Droid-Fu詳解 For Android: BetterActivity, BetterService And BetterAsyncTask
  下一篇:go Action Bar for Android