Android 15 से, Glance पर गड़बड़ी मैनेज करने की सुविधा को बेहतर बनाने के लिए, एपीआई की सुविधाएं शामिल की गई हैं. इस पेज पर, इन एपीआई के बारे में सबसे सही तरीके बताए गए हैं.
नॉन-कंपोज़ेबल कॉम्पोनेंट के लिए, try-catch ब्लॉक का इस्तेमाल करना
Compose, कंपोज़ेबल के लिए try-catch ब्लॉक की अनुमति नहीं देता. हालांकि, यह आपको इन ब्लॉक में अपने ऐप्लिकेशन की अन्य लॉजिक को रैप करने की अनुमति देता है. इससे, गड़बड़ी दिखाने वाले व्यू के लिए Compose का इस्तेमाल किया जा सकता है. जैसा कि यहां दिए गए उदाहरण में दिखाया गया है:
provideContent { var isError = false var data = listOf<String>() try { val repository = (context.applicationContext as MyApplication).myRepository data = repository.loadData() } catch (e: Exception) { isError = true // TODO: handle error } if (isError) { ErrorView() } else { Content(data) } }
गड़बड़ी दिखाने वाला डिफ़ॉल्ट लेआउट
अगर कोई ऐसी गड़बड़ी होती है जिसे मैनेज नहीं किया जा सका या Compose में कोई गड़बड़ी होती है, तो Glance गड़बड़ी दिखाने वाला डिफ़ॉल्ट लेआउट दिखाता है:
Glance, डेवलपर को फ़ॉलबैक के तौर पर एक्सएमएल लेआउट उपलब्ध कराने की अनुमति देता है. ऐसा तब किया जाता है, जब कंपोज़िशन में गड़बड़ी होती है. इसका मतलब है कि Compose कोड में कोई गड़बड़ी हुई है. अगर आपके ऐप्लिकेशन के कोड में कोई ऐसी गड़बड़ी होती है जिसे मैनेज नहीं किया जा सका, तो गड़बड़ी दिखाने वाला यह यूज़र इंटरफ़ेस (यूआई) भी दिखता है.
class UpgradeWidget : GlanceAppWidget(errorUiLayout = R.layout.error_layout) { // ... }
गड़बड़ी दिखाने वाले डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) में कार्रवाइयां जोड़ना
Glance 1.1.0 से, Glance आपको गड़बड़ी मैनेज करने वाले डिफ़ॉल्ट कोड को बदलने की अनुमति देता है. इस तरह, कंपोज़िशन में कोई ऐसी गड़बड़ी होने पर जिसे मैनेज नहीं किया जा सका या कोई अन्य गड़बड़ी होने पर, कार्रवाई के कॉलबैक जोड़े जा सकते हैं.
इस सुविधा का इस्तेमाल करने के लिए, onCompositionError() फ़ंक्शन को बदलें:
GlanceAppWidget.onCompositionError(
context: Context,
glanceId: GlanceId,
appWidgetId: Int,
throwable: Throwable
)
इस फ़ंक्शन में, Glance गड़बड़ी मैनेज करने के लिए RemoteViews एपीआई का इस्तेमाल करता है.
इससे, एक्सएमएल का इस्तेमाल करके लेआउट और ऐक्शन हैंडलर तय किए जा सकते हैं.
यहां दिए गए उदाहरणों में, गड़बड़ी दिखाने वाला यूज़र इंटरफ़ेस (यूआई) बनाने का तरीका चरण दर चरण बताया गया है. इसमें, सुझाव/राय या शिकायत भेजने के लिए एक बटन शामिल है:
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>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) }
एक पेंडिंग इंटेंट बनाएं, जो आपके
GlanceAppWidgetReceiverको रेफ़र करता हो:private fun getErrorIntent(context: Context, throwable: Throwable): PendingIntent { val intent = Intent(context, UpgradeToHelloWorldPro::class.java) intent.action = "widgetError" return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) }
अपने
GlanceAppWidgetReceiverमें इंटेंट को मैनेज करें:override fun onReceive(context: Context, intent: Intent) { super.onReceive(context, intent) Log.e("ErrorOnClick", "Button was clicked.") }