當機和例外狀況 - Android SDK

本文件會概要說明使用 Android v3 專用的 Google Analytics (分析) SDK 所進行的當機和例外狀況評估。

總覽

您可以透過當機和例外狀況評估,評估應用程式偵測到/未偵測到的當機和未擷取到當機情形和例外狀況的數量與類型。例外狀況有下列欄位:

欄位名稱 追蹤器欄位 類型 必要 說明
說明 Fields.EX_DESCRIPTION String 例外狀況的說明 (最多 100 個半形字元)。接受 null .
isFatal Fields.EX_FATAL boolean 指出例外狀況是否為嚴重例外狀況。true 表示嚴重。

系統主要會在「當機和例外狀況」報表中提供當機和例外狀況資料。

偵測到例外狀況

「捕捉到的例外狀況」是指您在應用程式中定義了例外狀況處理程式碼的錯誤,例如在資料要求期間偶爾發生網路連線逾時。

透過在追蹤程式上設定例外狀況欄位值並傳送命中,以評估偵測到的例外狀況,如以下範例所示:

/*
 * An app tries to load a list of high scores from the cloud. If the request
 * times out, an exception is sent to Google Analytics
 */
try {

  // Request some scores from the network.
  ArrayList highScores = getHighScoresFromCloud();

} catch (IOException e) {

  // May return null if EasyTracker has not yet been initialized with a
  // property ID.
  EasyTracker easyTracker = EasyTracker.getInstance(this);

  // StandardExceptionParser is provided to help get meaningful Exception descriptions.
 	easyTracker.send(MapBuilder
      .createException(new StandardExceptionParser(this, null)              // Context and optional collection of package names
                                                                            // to be used in reporting the exception.
                       .getDescription(Thread.currentThread().getName(),    // The name of the thread on which the exception occurred.
                                       e),                                  // The exception.
                       false)                                               // False indicates a nonfatal exception
      .build()
  );


  ... // Display alert to user that high scores are currently unavailable.
}

未偵測到例外狀況評估

未偵測到的例外狀況代表應用程式在執行階段遇到意外狀況,且經常嚴重導致應用程式異常終止的情況。您可以使用 EasyTracker 或 ExceptionReporter 類別,將未擷取的例外狀況自動傳送至 Google Analytics (分析)。

使用 EasyTracker

如要使用 EasyTracker 自動傳送應用程式中的所有未偵測到的例外狀況,請在 analytics.xml 檔案中新增以下這一行:

<bool name="ga_reportUncaughtExceptions">true</bool>

使用自動例外狀況評估功能傳送例外狀況後,EasyTracker 會將例外狀況傳送至 Thread 的預設例外狀況處理常式。

使用進階導入

如果您使用的是進階實作,而非使用 EasyTracker,請使用 ExceptionReporter 類別實作自動擷取的例外狀況評估。

ExceptionReporter 可做為特定執行緒或應用程式中所有執行緒的預設未偵測到的例外狀況處理常式。將例外狀況傳送至 Google Analytics (分析) 後,ExceptionReporter 類別可選擇將例外狀況傳遞給您提供的未偵測到的例外狀況處理常式。

下列程式碼會建立新的 ExceptionReporter 物件,並將其設為新的預設未偵測到的例外狀況處理常式。因此,每個未偵測到的例外狀況都會傳送至 Google Analytics (分析),然後再傳送至先前未偵測到的例外狀況處理常式。對於大多數應用程式,預設處理常式會將例外狀況記錄至記錄並終止應用程式。

UncaughtExceptionHandler myHandler = new ExceptionReporter(
    GoogleAnalytics.getInstance(this).getDefaultTracker(), // Tracker, may return null if not yet initialized.
    GAServiceManager.getInstance(),                        // GAServiceManager singleton.
    Thread.getDefaultUncaughtExceptionHandler());          // Current default uncaught exception handler.

// Make myHandler the new default uncaught exception handler.
Thread.setDefaultUncaughtExceptionHandler(myHandler);

使用自動例外狀況評估時,請注意下列事項:

  • 凡是使用自動例外狀況評估功能傳送的例外狀況,Google Analytics (分析) 都會將其回報為嚴重錯誤。
  • 根據預設,系統會透過例外狀況類型、類別名稱、方法名稱和執行緒名稱自動設定說明欄位。

剖析例外狀況說明

SDK 提供 StandardExceptionParser,可簡化取得例外狀況說明並傳送至 Google Analytics (分析) 的程序:

// Using StandardExceptionParser to get an Exception description.
try {

  // Request some scores from the network.
  ArrayList highScores = getHighScoresFromCloud();

} catch (IOException e) {

  // May return null if EasyTracker has not yet been initialized with a
  // property ID.
  EasyTracker easyTracker = EasyTracker.getInstance(this);

 	easyTracker.send(MapBuilder
      .createException(new StandardExceptionParser(this, null)              // Context and optional collection of package names
                                                                            // to be used in reporting the exception.
                       .getDescription(Thread.currentThread().getName(),    // The name of the thread on which the exception occurred.
                                       e),                                  // The exception.
	                     false)                                               // False indicates a fatal exception
      .build()
  );

  ... // Display alert to user that high scores are currently unavailable.
}

您也可以導入 ExceptionParser 介面,並在傳送例外狀況給 Google Analytics (分析) 時呼叫其 setDescription 方法來導入自己的剖析器。