Xử lý lỗi bằng tính năng Glance

Các tính năng API để cải thiện khả năng xử lý lỗi trên Glance đã được đưa vào Android 15. Trang này cung cấp một số phương pháp hay nhất liên quan đến các API này.

Sử dụng khối try-catch xung quanh các thành phần không phải thành phần kết hợp

Compose không cho phép các khối try-catch xung quanh các thành phần kết hợp, nhưng cho phép bạn gói logic khác của ứng dụng trong các khối này. Việc này cho phép bạn sử dụng Compose cho khung hiển thị lỗi, như trong ví dụ sau:

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)
       }
   }

Bố cục lỗi mặc định

Nếu có trường hợp ngoại lệ chưa nắm bắt được hoặc lỗi Compose, tính năng Glance sẽ hiển thị bố cục lỗi mặc định:

Một thông báo lỗi cho thấy loại lỗi và đề xuất về nơi cần tìm lỗi đó
Hình 1.Bố cục lỗi mặc định của Glance 1.0
Hộp có dòng chữ "Không thể hiển thị nội dung"
Hình 2. Bố cục lỗi mặc định của xem nhanh 1.1.0

Glance cho phép nhà phát triển cung cấp bố cục XML làm phương án dự phòng trong trường hợp thành phần kết hợp không thành công. Điều này có nghĩa là đã xảy ra lỗi trong mã Compose. Giao diện người dùng lỗi này cũng xuất hiện nếu bạn có lỗi chưa nắm bắt được trong mã của ứng dụng.

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

Bố cục này là bố cục tĩnh mà người dùng của bạn không tương tác được nhưng phù hợp trong trường hợp khẩn cấp.

Chứa tiêu đề và trường văn bản để cho thấy thông báo lỗi
Hình 3. Ví dụ về bố cục lỗi tuỳ chỉnh

Thêm thao tác vào giao diện người dùng mặc định báo lỗi

Kể từ Glance 1.1.0, Glance cho phép bạn ghi đè mã xử lý lỗi mặc định. Bằng cách này, bạn có thể thêm các lệnh gọi lại hành động trong trường hợp có trường hợp ngoại lệ hoặc lỗi chưa nắm bắt được trong cấu trúc.

Để sử dụng tính năng này, hãy ghi đè hàm onCompositionError():

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

Trong hàm này, Glance sẽ quay lại API RemoteViews để xử lý lỗi. Điều này cho phép bạn chỉ định bố cục và trình xử lý thao tác bằng XML.

Các ví dụ sau đây hướng dẫn từng bước cách tạo một giao diện người dùng báo lỗi có chứa một nút để gửi ý kiến phản hồi:

  1. Viết tệp 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. Ghi đè hàm 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. Tạo một ý định đang chờ xử lý tham chiếu đến GlanceAppWidgetReceiver của bạn
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. Xử lý ý định trong GlanceAppWidgetReceiver
override fun onReceive(context: Context, intent: Intent) {
   super.onReceive(context, intent)
   Log.e("ErrorOnClick", "Button was clicked.");
}