使用 Glance 處理錯誤

從 Android 15 開始,我們會提供能改善 Glance 錯誤處理機制的 API 功能。本頁面提供關於這些 API 的最佳做法。

在非可組合函式周圍使用 try-catch 區塊

Compose 不允許在可組合項周圍使用 try-catch 區塊,但您可以將應用程式的其他邏輯納入這些區塊。這樣您就能使用 Compose 處理錯誤檢視畫面,如以下範例所示:

provideContent {
       var isError = false;
       var data = null
       try {
           val repository = (context.applicationContext as MyApplication).myRepository
           data = repository.loadData()
       } catch (e: Exception) {
           isError = true;
           //handleError
       }

       if (isError) {
           ErrorView()
       } else {
           Content(data)
       }
   }

預設錯誤版面配置

如果遇到未擷取的例外狀況或 Compose 錯誤,Glance 會顯示預設的錯誤版面配置:

錯誤訊息顯示錯誤類型,並提供查看位置的建議
圖 1.Glance 1.0 預設錯誤版面配置
附有「無法顯示內容」的方塊
圖 2.Glance 1.1.0 預設錯誤版面配置

如果組合失敗,開發人員可以透過 Glance 提供 XML 版面配置做為備用方案。這表示 Compose 程式碼有誤。如果應用程式的程式碼中有未偵測到的錯誤,也會顯示這個錯誤 UI。

class UpgradeWidget : GlanceAppWidget(errorUiLayout = R.layout.error_layout)

這個版面配置是無法與使用者互動的靜態版面配置,但在緊急情況下非常實用。

包含標題和顯示錯誤訊息的文字欄位
圖 3:自訂錯誤版面配置範例

在預設錯誤 UI 中新增動作

從 Glance 1.1.0 開始,您可覆寫預設的錯誤處理程式碼。如此一來,您就可以在組合中未偵測到的例外狀況或錯誤時,新增動作回呼。

如要使用這項功能,請覆寫 onCompositionError() 函式:

GlanceAppWidget.onCompositionError(
    context: Context,
    glanceId: GlanceId,
    AppWidgetId: Int,
    throwable: Throwable
)

在此函式中,Glance 會改回使用 RemoteViews API 處理錯誤。這可讓您使用 XML 指定版面配置和動作處理常式。

下列範例將逐步說明如何建立錯誤 UI,其中包含用來傳送意見回饋的按鈕:

  1. 寫入 error_layout.xml 檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   style="@style/Widget.MyApplication.AppWidget.Error"
   android:id="@android:id/background"
   android:layout_width="match_parent"
   android:textSize="24sp"
   android:layout_height="match_parent"
   android:orientation="vertical">


   <TextView
       android:id="@+id/error_title_view"
       android:layout_width="match_parent"
       android:textColor="@color/white"
       android:textFontWeight="800"
       android:layout_height="wrap_content"
       android:text="Example Widget Error" />

   <LinearLayout
       android:layout_width="match_parent"
       android:orientation="horizontal"
       android:paddingTop="4dp"
       android:layout_height="match_parent">

       <ImageButton
           android:layout_width="64dp"
           android:layout_height="64dp"
           android:layout_gravity="center"
           android:tint="@color/white"
           android:id="@+id/error_icon"
           android:src="@drawable/heart_broken_fill0_wght400_grad0_opsz24"
       />
       <TextView
           android:id="@+id/error_text_view"
           android:layout_width="wrap_content"
           android:textColor="@color/white"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:padding="8dp"
           android:textSize="16sp"
           android:layout_weight="1"
           android:text="Useful Error Message!" />
   </LinearLayout>

</LinearLayout>

  1. 覆寫 onCompositionError 函式
override fun onCompositionError(
   context: Context,
   glanceId: GlanceId,
   AppWidgetId: Int,
   throwable: Throwable
) {
   val rv = RemoteViews(context.packageName, R.layout.error_layout)
   rv.setTextViewText(
       R.id.error_text_view,
       "Error was thrown. \nThis is a custom view \nError Message: `${throwable.message}`"
   )
   rv.setOnClickPendingIntent(R.id.error_icon, getErrorIntent(context, throwable))
   AppWidgetManager.getInstance(context).updateAppWidget(AppWidgetId, rv)
}

  1. 建立參照 GlanceAppWidgetReceiver 的待處理意圖
private fun getErrorIntent(context: Context, throwable: Throwable): PendingIntent {
    val intent = Intent(context, UpgradeToHelloWorldPro::class.java)
    intent.setAction("widgetError")
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}

  1. GlanceAppWidgetReceiver 中處理意圖
override fun onReceive(context: Context, intent: Intent) {
   super.onReceive(context, intent)
   Log.e("ErrorOnClick", "Button was clicked.");
}