קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
תכונות API לשיפור הטיפול בשגיאות ב-Glance כלולות החל מ-Android 15. בדף הזה מפורטות שיטות מומלצות לשימוש בממשקי ה-API האלה.
שימוש בבלוק try-catch סביב רכיבים שאי אפשר להרכיב
Compose לא מאפשרת להשתמש בבלוקים של try-catch סביב פונקציות Composable, אבל מאפשרת להשתמש בבלוקים האלה כדי לעטוף את הלוגיקה האחרת של האפליקציה. כך תוכלו להשתמש ב-Compose לתצוגת השגיאות, כמו בדוגמה הבאה:
Glance מאפשר למפתחים לספק פריסת XML כגיבוי אם ההרכבה נכשלת. המשמעות היא שהייתה שגיאה בקוד של Compose. ממשק המשתמש של השגיאה הזו מופיע גם אם יש שגיאה לא מטופלת בקוד של האפליקציה.
הפריסה הזו היא פריסה סטטית שהמשתמש לא יכול ליצור איתה אינטראקציה, אבל היא טובה במקרי חירום.
איור 3. דוגמה לפריסת שגיאה בהתאמה אישית
הוספת פעולות לממשק המשתמש של שגיאות שמוגדר כברירת מחדל
החל מגרסה 1.1.0 של Glance, אפשר לבטל את קוד הטיפול בשגיאות שמוגדר כברירת מחדל.
כך תוכלו להוסיף קריאות חוזרות (callback) לפעולה במקרה של חריגה לא מטופלת או שגיאה בהרכבה.
כדי להשתמש בתכונה הזו, צריך לבטל את ההגדרה של הפונקציה onCompositionError():
overridefunonCompositionError(context:Context,glanceId:GlanceId,appWidgetId:Int,throwable:Throwable){valrv=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)}
יוצרים כוונת רכישה בהמתנה שמפנה אל GlanceAppWidgetReceiver:
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-27 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-08-27 (שעון UTC)."],[],[],null,["API features for improving error handling on Glance are included beginning in\nAndroid 15. This page provides best practices regarding these APIs.\n\nUse a try-catch block around non-composable components\n\nCompose doesn't allow try-catch blocks around composables, but lets you wrap\nyour app's other logic in these blocks. This lets you use Compose for your\nerror view, as shown in the following example: \n\n provideContent {\n var isError = false;\n var data = null\n try {\n val repository = (context.applicationContext as MyApplication).myRepository\n data = repository.loadData()\n } catch (e: Exception) {\n isError = true;\n //handleError\n }\n\n if (isError) {\n ErrorView()\n } else {\n Content(data)\n }\n }\n\nDefault error layout\n\nIf there is an uncaught exception or a Compose error, Glance displays a\ndefault error layout:\n**Figure 1.**Glance 1.0 default error layout **Figure 2.**Glance 1.1.0 default error layout\n\nGlance lets developers provide an XML layout as a fallback if composition\nfails. This means that there was an error in the Compose code. This error UI\nalso appears if you have an uncaught error in your app's code. \n\n class UpgradeWidget : GlanceAppWidget(errorUiLayout = R.layout.error_layout)\n\nThis layout is a static layout that your user can't interact with, but is good\nin emergency cases.\n**Figure 3.**Example custom error layout\n\nAdd actions to the default error UI\n\nAs of Glance 1.1.0, Glance lets you override the default error handling code.\nThis way, you can add action callbacks in the event of an uncaught exception or\nerror in composition.\n\nTo use this feature, override the `onCompositionError()` function: \n\n GlanceAppWidget.onCompositionError(\n context: Context,\n glanceId: GlanceId,\n appWidgetId: Int,\n throwable: Throwable\n )\n\nIn this function, Glance falls back to the `RemoteViews` API for error handling.\nThis lets you specify layouts and action handlers using XML.\n\nThe following examples show you, step-by-step, how to create an error UI that\nincludes a button to send feedback:\n\n1. Write the `error_layout.xml` file:\n\n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n style=\"@style/Widget.MyApplication.AppWidget.Error\"\n android:id=\"@android:id/background\"\n android:layout_width=\"match_parent\"\n android:textSize=\"24sp\"\n android:layout_height=\"match_parent\"\n android:orientation=\"vertical\"\u003e\n\n \u003cTextView\n android:id=\"@+id/error_title_view\"\n android:layout_width=\"match_parent\"\n android:textColor=\"@color/white\"\n android:textFontWeight=\"800\"\n android:layout_height=\"wrap_content\"\n android:text=\"Example Widget Error\" /\u003e\n\n \u003cLinearLayout\n android:layout_width=\"match_parent\"\n android:orientation=\"horizontal\"\n android:paddingTop=\"4dp\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cImageButton\n android:layout_width=\"64dp\"\n android:layout_height=\"64dp\"\n android:layout_gravity=\"center\"\n android:tint=\"@color/white\"\n android:id=\"@+id/error_icon\"\n android:src=\"@drawable/heart_broken_fill0_wght400_grad0_opsz24\"\n /\u003e\n \u003cTextView\n android:id=\"@+id/error_text_view\"\n android:layout_width=\"wrap_content\"\n android:textColor=\"@color/white\"\n android:layout_height=\"wrap_content\"\n android:layout_gravity=\"center\"\n android:padding=\"8dp\"\n android:textSize=\"16sp\"\n android:layout_weight=\"1\"\n android:text=\"Useful Error Message!\" /\u003e\n \u003c/LinearLayout\u003e\n\n \u003c/LinearLayout\u003e\n\n2. Override the `onCompositionError` function:\n\n override fun onCompositionError(\n context: Context,\n glanceId: GlanceId,\n appWidgetId: Int,\n throwable: Throwable\n ) {\n val rv = RemoteViews(context.packageName, R.layout.error_layout)\n rv.setTextViewText(\n R.id.error_text_view,\n \"Error was thrown. \\nThis is a custom view \\nError Message: `${throwable.message}`\"\n )\n rv.setOnClickPendingIntent(R.id.error_icon, getErrorIntent(context, throwable))\n AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, rv)\n }\n\n3. Create a pending intent that references your `GlanceAppWidgetReceiver`:\n\n private fun getErrorIntent(context: Context, throwable: Throwable): PendingIntent {\n val intent = Intent(context, UpgradeToHelloWorldPro::class.java)\n intent.setAction(\"widgetError\")\n return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)\n }\n\n4. Handle the intent in your `GlanceAppWidgetReceiver`:\n\n override fun onReceive(context: Context, intent: Intent) {\n super.onReceive(context, intent)\n Log.e(\"ErrorOnClick\", \"Button was clicked.\");\n }"]]