एक नज़र में जानकारी देखने की सुविधा की मदद से गड़बड़ियां मैनेज करना

एक नज़र में जानकारी देखने की सुविधा को बेहतर बनाने के लिए, एपीआई की सुविधाओं की शुरुआत Android 15. इस पेज पर, इन एपीआई के बारे में कुछ सबसे सही तरीके बताए गए हैं.

कंपोज़ेबल कॉम्पोनेंट के आस-पास ट्राई कैच ब्लॉक का इस्तेमाल करें

कंपोज़ेबल में मौजूद 'ट्राई-कैच ब्लॉक' सुविधा को अनुमति नहीं दी गई है, लेकिन यह आपको कंपोज़ेबल में रैप कर देता है ऐप्लिकेशन इस्तेमाल करने के लिए बने दूसरे लॉजिक को भी ध्यान में रखें. इससे आपको नीचे दी गई, गड़बड़ी व्यू, जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है:

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

गड़बड़ी का डिफ़ॉल्ट लेआउट

अगर कोई गड़बड़ी मिलती है या मैसेज लिखने में कोई गड़बड़ी होती है, तो एक नज़र में गड़बड़ी का डिफ़ॉल्ट लेआउट:

गड़बड़ी का टाइप दिखाने वाला गड़बड़ी का मैसेज और कहां के लिए सुझाव
  इसे देखने के लिए
पहली इमेज.Glance 1.0 का डिफ़ॉल्ट गड़बड़ी वाला लेआउट
'कॉन्टेंट नहीं दिखाया जा सकता' टेक्स्ट वाला बॉक्स
दूसरी इमेज.Glance 1.1.0 का डिफ़ॉल्ट गड़बड़ी वाला लेआउट

अगर कंपोज़िशन में जानकारी दी गई है, तो एक नज़र में जानकारी देने की सुविधा की मदद से डेवलपर, एक्सएमएल लेआउट को फ़ॉलबैक के तौर पर उपलब्ध करा सकते हैं विफल होता है. इसका मतलब है कि लिखें कोड में कोई गड़बड़ी थी. गड़बड़ी वाला यह यूज़र इंटरफ़ेस (यूआई) यह भी तब दिखता है, जब आपके ऐप्लिकेशन के कोड में कोई गड़बड़ी होती है.

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

यह लेआउट एक स्टैटिक लेआउट है, जिससे आपका उपयोगकर्ता इंटरैक्ट नहीं कर सकता, लेकिन यह अच्छा है आपातकालीन मामलों में.

इसमें गड़बड़ी का मैसेज दिखाने के लिए, हेडिंग और टेक्स्ट फ़ील्ड है
तीसरी इमेज.कस्टम गड़बड़ी वाले लेआउट का उदाहरण

डिफ़ॉल्ट गड़बड़ी वाले यूज़र इंटरफ़ेस (यूआई) में कार्रवाइयां जोड़ें

एक नज़र में जानकारी देखने की सुविधा के 1.1.0 वर्शन से, गड़बड़ी को मैनेज करने वाले डिफ़ॉल्ट कोड को बदला जा सकता है. इस तरह, ऐसे अपवाद होने पर ऐक्शन कॉलबैक जोड़े जा सकते हैं जिनके बारे में पता नहीं चला है या कंपोज़िशन में गड़बड़ी है.

इस सुविधा का इस्तेमाल करने के लिए, onCompositionError() फ़ंक्शन को बदलें:

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

इस फ़ंक्शन में, गड़बड़ी ठीक करने के लिए एक नज़र में जानकारी देखने की सुविधा RemoteViews एपीआई पर वापस चली जाती है. इसकी मदद से, एक्सएमएल का इस्तेमाल करके लेआउट और ऐक्शन हैंडलर तय किए जा सकते हैं.

नीचे दिए गए उदाहरणों में, गड़बड़ी वाला यूज़र इंटरफ़ेस (यूआई) बनाने के सिलसिलेवार तरीके बताए गए हैं सुझाव, शिकायत या राय भेजने का बटन मौजूद होता है:

  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 का रेफ़रंस मिलता हो
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 में इंटेंट को हैंडल करें
override fun onReceive(context: Context, intent: Intent) {
   super.onReceive(context, intent)
   Log.e("ErrorOnClick", "Button was clicked.");
}