ऐडवांस विजेट बनाएं

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

विजेट का कॉन्टेंट अपडेट करने के लिए ऑप्टिमाइज़ेशन

विजेट का कॉन्टेंट अपडेट करना, कंप्यूटर की मदद से महंगा हो सकता है. बैटरी बचाने के लिए इस्तेमाल करना, अपडेट टाइप, फ़्रीक्वेंसी, और समय को ऑप्टिमाइज़ करना.

विजेट अपडेट के टाइप

विजेट को तीन तरीकों से अपडेट किया जा सकता है: पूरा अपडेट, आंशिक अपडेट, और का इस्तेमाल, डेटा रीफ़्रेश करने के लिए किया जा सकता है. हर स्थिति अलग होती है लागत और जटिलताओं के बारे में बताएं.

नीचे हर अपडेट टाइप के बारे में बताया गया है और हर अपडेट के लिए कोड स्निपेट दिए गए हैं.

  • पूरा अपडेट: AppWidgetManager.updateAppWidget(int, android.widget.RemoteViews) पर कॉल करें विजेट को पूरी तरह से अपडेट करने के लिए. यह पहले दिए गए टूल की जगह ले लेता है RemoteViews RemoteViews. यह कंप्यूटेशनल तौर पर सबसे महंगा अपडेट है.

    Kotlin

    val appWidgetManager = AppWidgetManager.getInstance(context)
    val remoteViews = RemoteViews(context.getPackageName(), R.layout.widgetlayout).also {
    setTextViewText(R.id.textview_widget_layout1, "Updated text1")
    setTextViewText(R.id.textview_widget_layout2, "Updated text2")
    }
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
    

    Java

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
    remoteViews.setTextViewText(R.id.textview_widget_layout1, "Updated text1");
    remoteViews.setTextViewText(R.id.textview_widget_layout2, "Updated text2");
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    
  • आंशिक अपडेट: कॉल AppWidgetManager.partiallyUpdateAppWidget अपडेट करने के लिए. यह नए RemoteViews को पहले उपलब्ध कराया गया RemoteViews. अगर एक विजेट updateAppWidget(int[], RemoteViews) तक कम से कम एक पूरा अपडेट नहीं मिलता.

    Kotlin

    val appWidgetManager = AppWidgetManager.getInstance(context)
    val remoteViews = RemoteViews(context.getPackageName(), R.layout.widgetlayout).also {
    setTextViewText(R.id.textview_widget_layout, "Updated text")
    }
    appWidgetManager.partiallyUpdateAppWidget(appWidgetId, remoteViews)
    

    Java

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
    remoteViews.setTextViewText(R.id.textview_widget_layout, "Updated text");
    appWidgetManager.partiallyUpdateAppWidget(appWidgetId, remoteViews);
    
  • कलेक्शन डेटा रीफ़्रेश: कॉल AppWidgetManager.notifyAppWidgetViewDataChanged का इस्तेमाल करें. यह ट्रिगर होता है RemoteViewsFactory.onDataSetChanged. इस दौरान, पुराना डेटा विजेट में दिखता है. सुरक्षित तरीके से इस तरीके की मदद से महंगे काम एक ही समय पर पूरे किए जा सकेंगे.

    Kotlin

    val appWidgetManager = AppWidgetManager.getInstance(context)
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_listview)
    

    Java

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_listview);
    

इन तरीकों को अपने ऐप्लिकेशन में कहीं से भी कॉल किया जा सकता है. इसके लिए, यह ज़रूरी है कि ऐप्लिकेशन में संबंधित यूआईडी ही AppWidgetProvider क्लास.

तय करें कि किसी विजेट को कितनी बार अपडेट करना है

विजेट को समय-समय पर, updatePeriodMillis एट्रिब्यूट की वैल्यू सबमिट करें. उपयोगकर्ता इंटरैक्शन के जवाब में विजेट अपडेट हो सकता है, ब्रॉडकास्ट अपडेट या फिर दोनों का इस्तेमाल करें.

समय-समय पर अपडेट करें

आप समय-समय पर होने वाले अपडेट की फ़्रीक्वेंसी को appwidget-provider एक्सएमएल में AppWidgetProviderInfo.updatePeriodMillis. हर अपडेट, AppWidgetProvider.onUpdate() तरीके को ट्रिगर करता है, जहां पर विजेट को अपडेट करने के लिए कोड डाल सकता है. हालांकि, के लिए विकल्पों ब्रॉडकास्ट रिसीवर में बताए गए अपडेट यदि विजेट को एसिंक्रोनस रूप से डेटा लोड करने की आवश्यकता है या 10 सेकंड से कम समय में अपडेट होता है, क्योंकि 10 सेकंड के बाद, सिस्टम जवाब न देने के लिए BroadcastReceiver.

updatePeriodMillis में, 30 मिनट से कम की वैल्यू इस्तेमाल नहीं की जा सकतीं. हालांकि, अगर अगर आप समय-समय पर मिलने वाले अपडेट को बंद करना चाहते हैं, तो आप 0 तय कर सकते हैं.

कॉन्फ़िगरेशन में, उपयोगकर्ताओं को यह सुविधा दी जा सकती है कि वे अपडेट की फ़्रीक्वेंसी में बदलाव कर सकें. इसके लिए उदाहरण के लिए, हो सकता है कि वे स्टॉक टिकर को हर 15 मिनट या सिर्फ़ चार बार अपडेट करना चाहें दिन में कई बार. इस मामले में, updatePeriodMillis को 0 पर सेट करें और इसके बजाय, WorkManager का इस्तेमाल करें.

उपयोगकर्ता इंटरैक्शन के जवाब में अपडेट करें

लोगों के इंटरैक्शन के आधार पर, विजेट को अपडेट करने के कुछ तरीके यहां सुझाए गए हैं:

  • ऐप्लिकेशन की किसी गतिविधि से: सीधे कॉल करें उपयोगकर्ता इंटरैक्शन के जवाब में AppWidgetManager.updateAppWidget, जैसे टैप किया जा सकता है.

  • रिमोट इंटरैक्शन, जैसे कि सूचना या ऐप्लिकेशन के विजेट से: PendingIntent बनाएं. इसके बाद, लागू किए गए विजेट में से विजेट को अपडेट करें Activity, Broadcast या Service. अपने हिसाब से प्राथमिकता चुनी जा सकती है. इसके लिए उदाहरण के लिए, अगर PendingIntent के लिए Broadcast चुना जाता है, तो एक फ़ोरग्राउंड ब्रॉडकास्ट BroadcastReceiver प्राथमिकता.

ब्रॉडकास्ट इवेंट के जवाब में अपडेट करें

एक ऐसे ब्रॉडकास्ट इवेंट का उदाहरण होता है जिसे अपडेट करने के लिए विजेट की ज़रूरत होती है. ऐसा तब होता है, जब जब कोई व्यक्ति फ़ोटो लेता है. ऐसे मामले में, नई फ़ोटो होने पर आप विजेट को अपडेट करना चाहते हैं का पता चला है.

JobScheduler के साथ काम शेड्यूल किया जा सकता है. साथ ही, यह भी बताया जा सकता है कि का इस्तेमाल करके ट्रिगर करें JobInfo.Builder.addTriggerContentUri तरीका.

ब्रॉडकास्ट के लिए BroadcastReceiver भी रजिस्टर किया जा सकता है—उदाहरण के लिए, यह सुन रही हूँ ACTION_LOCALE_CHANGED. हालांकि, इसमें डिवाइस के संसाधनों का इस्तेमाल होता है. इसलिए, इसका इस्तेमाल सावधानी से करें और ध्यान से सुनें सिर्फ़ खास ब्रॉडकास्ट पर. ब्रॉडकास्ट की सुविधा लॉन्च की गई Android में सीमाएं 7.0 (एपीआई लेवल 24) और Android 8.0 (एपीआई लेवल 26) के लिए, ऐप्लिकेशन इंप्लिसिट वर्शन रजिस्टर नहीं कर सकते कुछ चीज़ों के साथ-साथ, अपवाद.

BroadcastReceiver से विजेट अपडेट करते समय ध्यान रखने वाली बातें

अगर विजेट को BroadcastReceiver से अपडेट किया जाता है, तो इसमें ये भी शामिल हैं AppWidgetProvider को ध्यान रखें. इन बातों का ध्यान रखें: विजेट के अपडेट होने की अवधि और प्राथमिकता.

अपडेट की अवधि

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

  • WorkManager का इस्तेमाल करके टास्क शेड्यूल करें.

  • पैसे पाने वाले व्यक्ति को इसकी मदद से ज़्यादा समय दें goAsync तरीका. इससे रिसीवर 30 सेकंड तक काम करता है.

सुरक्षा से जुड़ी खास बातें और सबसे सही सुझाव देखें तरीके जानकारी.

अपडेट की प्राथमिकता

डिफ़ॉल्ट रूप से, ब्रॉडकास्ट AppWidgetProvider.onUpdate—बैकग्राउंड प्रोसेस के तौर पर चलता है. इसका मतलब है सिस्टम के बहुत ज़्यादा संसाधन लोड होने की वजह से, ब्रॉडकास्ट को शुरू करने में देरी हो सकती है पाने वाले. ब्रॉडकास्ट को प्राथमिकता देने के लिए, उसे फ़ोरग्राउंड प्रोसेस बनाएं.

उदाहरण के लिए, Intent.FLAG_RECEIVER_FOREGROUND उपयोगकर्ता के ज़रिए PendingIntent.getBroadcast को भेजे गए Intent के लिए फ़्लैग किया गया विजेट के किसी खास हिस्से पर टैप करता है.

डाइनैमिक आइटम वाली सटीक झलक बनाएं

पहली इमेज: विजेट की झलक, जिसमें सूची में कोई आइटम नहीं दिख रहा है.

यह सेक्शन कलेक्शन वाले विजेट के विजेट की झलक देखें—यानी कि एक ऐसा विजेट जो ListView, GridView या StackView.

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

कलेक्शन व्यू वाले विजेट की झलक, विजेट में ठीक से दिखे पिकर, तो हम एक अलग लेआउट फ़ाइल बनाए रखने का सुझाव देते हैं, जो झलक देखें. इस अलग लेआउट फ़ाइल में असल विजेट लेआउट और नकली आइटम वाला प्लेसहोल्डर कलेक्शन व्यू. उदाहरण के लिए, कई नकली सूची के साथ LinearLayout प्लेसहोल्डर देकर ListView आइटम.

ListView का उदाहरण दिखाने के लिए, एक अलग लेआउट फ़ाइल से शुरुआत करें:

// res/layout/widget_preview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/widget_background"
   android:orientation="vertical">

    // Include the actual widget layout that contains ListView.
    <include
        layout="@layout/widget_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    // The number of fake items you include depends on the values you provide
    // for minHeight or targetCellHeight in the AppWidgetProviderInfo
    // definition.

    <TextView android:text="@string/fake_item1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="?attr/appWidgetInternalPadding" />

    <TextView android:text="@string/fake_item2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="?attr/appWidgetInternalPadding" />

</LinearLayout>

इसकी previewLayout एट्रिब्यूट की वैल्यू देते समय, झलक लेआउट वाली फ़ाइल तय करें AppWidgetProviderInfo मेटाडेटा. आप अब भी वास्तविक विजेट लेआउट तय करते हैं initialLayout एट्रिब्यूट के लिए सेट करें. साथ ही, इस समय असल विजेट लेआउट का इस्तेमाल करें रनटाइम पर RemoteViews बनाना.

<appwidget-provider
    previewLayout="@layout/widget_previe"
    initialLayout="@layout/widget_view" />

कॉम्प्लेक्स आइटम की सूची

पिछले सेक्शन में दिए गए उदाहरण में नकली सूची आइटम मौजूद हैं, क्योंकि आइटम, TextView ऑब्जेक्ट हैं. यह काम किया जा सकता है और जटिल लेआउट होने पर नकली आइटम देना और मुश्किल हो जाता है.

सूची में मौजूद किसी ऐसे आइटम का इस्तेमाल करें जिसके बारे में widget_list_item.xml में बताया गया है और जिसमें ये चीज़ें शामिल हैं दो TextView ऑब्जेक्ट:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <TextView android:id="@id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fake_title" />

    <TextView android:id="@id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fake_content" />
</LinearLayout>

नकली सूची आइटम उपलब्ध कराने के लिए, आप लेआउट को कई बार शामिल कर सकते हैं, लेकिन की वजह से हर सूची आइटम एक जैसा हो जाता है. खास सूची आइटम उपलब्ध कराने के लिए, फ़ॉलो करें यह तरीका अपनाएं:

  1. टेक्स्ट वैल्यू के लिए एट्रिब्यूट का सेट बनाएं:

    <resources>
        <attr name="widgetTitle" format="string" />
        <attr name="widgetContent" format="string" />
    </resources>
    
  2. टेक्स्ट सेट करने के लिए इन एट्रिब्यूट का इस्तेमाल करें:

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        <TextView android:id="@id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="?widgetTitle" />
    
        <TextView android:id="@id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="?widgetContent" />
    </LinearLayout>
    
  3. झलक के लिए ज़रूरत के मुताबिक स्टाइल बनाएं. इसमें मान फिर से तय करें हर स्टाइल:

    <resources>
    
        <style name="Theme.Widget.ListItem">
            <item name="widgetTitle"></item>
            <item name="widgetContent"></item>
        </style>
        <style name="Theme.Widget.ListItem.Preview1">
            <item name="widgetTitle">Fake Title 1</item>
            <item name="widgetContent">Fake content 1</item>
        </style>
        <style name="Theme.Widget.ListItem.Preview2">
            <item name="widgetTitle">Fake title 2</item>
            <item name="widgetContent">Fake content 2</item>
        </style>
    
    </resources>
    
  4. झलक दिखाने वाले लेआउट में नकली आइटम पर स्टाइल लागू करें:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" ...>
    
        <include layout="@layout/widget_view" ... />
    
        <include layout="@layout/widget_list_item"
            android:theme="@style/Theme.Widget.ListItem.Preview1" />
    
        <include layout="@layout/widget_list_item"
            android:theme="@style/Theme.Widget.ListItem.Preview2" />
    
    </LinearLayout>