با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
ویژگیهای API برای بهبود مدیریت خطا در Glance در Android 15 گنجانده شده است. این صفحه بهترین روشها را در مورد این APIها ارائه میکند.
از یک بلوک try-catch در اطراف اجزای غیرقابل ترکیب استفاده کنید
Compose به بلوکهای try-catch در اطراف Composableها اجازه نمیدهد، اما به شما امکان میدهد منطق دیگر برنامهتان را در این بلوکها بپیچید. این به شما امکان می دهد از Compose برای نمای خطای خود استفاده کنید، همانطور که در مثال زیر نشان داده شده است:
اگر یک استثنا کشف نشده یا یک خطای Compose وجود داشته باشد، Glance یک طرح خطای پیشفرض را نشان میدهد:
شکل 1. به طرح بندی خطای پیش فرض 1.0 نگاه کنید شکل 2. طرح بندی خطای پیش فرض 1.1.0 را نگاه کنید
Glance به توسعه دهندگان این امکان را می دهد که در صورت شکست ترکیب، یک طرح XML را به عنوان بازگشتی ارائه کنند. این به این معنی است که در کد Compose خطایی وجود داشته است. در صورتی که در کد برنامه خود خطای کشف نشده داشته باشید، این رابط کاربری خطا نیز ظاهر می شود.
این چیدمان یک چیدمان ثابت است که کاربر شما نمی تواند با آن تعامل داشته باشد، اما در موارد اضطراری خوب است.
شکل 3. نمونه طرح خطای سفارشی
اقدامات را به رابط کاربری خطای پیشفرض اضافه کنید
از Glance 1.1.0، Glance به شما امکان می دهد کد پیش فرض رسیدگی به خطا را لغو کنید. به این ترتیب، میتوانید در صورت بروز یک استثنا یا خطا در ترکیب، فراخوانهای عمل اضافه کنید.
برای استفاده از این ویژگی، تابع onCompositionError() را لغو کنید:
در این تابع، Glance برای مدیریت خطا به API RemoteViews برمی گردد. این به شما امکان میدهد طرحبندیها و کنترلکنندههای عملکرد را با استفاده از XML مشخص کنید.
مثالهای زیر به صورت گام به گام به شما نشان میدهند که چگونه یک رابط کاربری خطا ایجاد کنید که شامل یک دکمه برای ارسال بازخورد است:
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)}
یک intent معلق ایجاد کنید که به GlanceAppWidgetReceiver شما ارجاع دهد:
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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 بهوقت ساعت هماهنگ جهانی."],[],[],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 }"]]