使用 Glance 处理错误

用于改进 Glance 中的错误处理的 API 功能从 Android 15。本页介绍了有关这些 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 会显示 默认错误布局:

<ph type="x-smartling-placeholder">
</ph> 一条错误消息,显示错误类型和位置建议
  即可查找
图 1. Glance 1.0 默认错误布局
显示“Can&#39;t show content”(无法显示内容)字样的框
图 2. Glance 1.1.0 默认错误布局

Glance 允许开发者在组合时提供 XML 布局作为后备 失败。这意味着 Compose 代码中存在错误。此错误界面 。

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

此布局是用户无法与之互动的静态布局,但很好 在紧急情况下使用。

<ph type="x-smartling-placeholder">
</ph> 包含标题和用于显示错误消息的文本字段
图 3. 自定义错误布局示例

向默认错误界面添加操作

从 Glance 1.1.0 开始,Glance 允许您替换默认的错误处理代码。 这样,您就可以在出现未捕获的异常时添加操作回调,或者 组合错误

如需使用此功能,请替换 onCompositionError() 函数:

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

在此函数中,Glance 会回退到 RemoteViews API 来处理错误。 这样,您就可以使用 XML 指定布局和操作处理程序。

以下示例分步展示了如何创建错误界面 包含一个用于发送反馈的按钮:

  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 的待处理 intent
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 中处理 intent
override fun onReceive(context: Context, intent: Intent) {
   super.onReceive(context, intent)
   Log.e("ErrorOnClick", "Button was clicked.");
}