2012-12-04 4 views
18

यह मुझे पागल कर रहा है। मुझे नहीं पता कि अनुशंसित प्रथाओं के साथ भी कॉन्फ़िगरेशन गतिविधि से ऐप विजेट को कैसे अपडेट किया जाए। ऐप विजेट निर्माण पर अपडेट विधि क्यों नहीं कहा जाता है मेरी समझ से परे है।कॉन्फ़िगरेशन गतिविधि के साथ ऐप विजेट कैसे बनाएं, और इसे पहली बार अपडेट करें?

मुझे क्या चाहिए: एक ऐप विजेट जिसमें आइटम संग्रह (एक सूचीदृश्य के साथ) होता है। लेकिन उपयोगकर्ता को कुछ चुनने की जरूरत है, इसलिए मुझे एक कॉन्फ़िगरेशन गतिविधि की आवश्यकता है।

विन्यास गतिविधि एक ListActivity है:

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class ChecksWidgetConfigureActivity extends SherlockListActivity { 
    private List<Long> mRowIDs; 
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; 
    private BaseAdapter mAdapter; 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setResult(RESULT_CANCELED); 
     setContentView(R.layout.checks_widget_configure); 

     final Intent intent = getIntent(); 
     final Bundle extras = intent.getExtras(); 
     if (extras != null) { 
      mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
     } 

     // If they gave us an intent without the widget id, just bail. 
     if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { 
      finish(); 
     } 

     mRowIDs = new ArrayList<Long>(); // it's actually loaded from an ASyncTask, don't worry about that — it works. 
     mAdapter = new MyListAdapter((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
     getListView().setAdapter(mAdapter); 
    } 

    private class MyListAdapter extends BaseAdapter { 
     // not relevant... 
    } 

    @Override 
    protected void onListItemClick(final ListView l, final View v, final int position, final long id) { 
     if (position < mRowIDs.size()) { 
      // Set widget result 
      final Intent resultValue = new Intent(); 
      resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 
      resultValue.putExtra("rowId", mRowIDs.get(position)); 
      setResult(RESULT_OK, resultValue); 

      // Request widget update 
      final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
      ChecksWidgetProvider.updateAppWidget(this, appWidgetManager, mAppWidgetId, mRowIDs); 
     } 

     finish(); 
    } 
} 

आप देख सकते हैं मैं अपने ऐप विजेट प्रदाता से एक स्थिर विधि फोन कर रहा हूँ। मुझे यह विचार the official doc से मिला है।

मेरा प्रदाता पर एक नजर है:

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
public class ChecksWidgetProvider extends AppWidgetProvider { 
    public static final String TOAST_ACTION = "com.example.android.stackwidget.TOAST_ACTION"; 
    public static final String EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM"; 

    @Override 
    public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) { 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 
     final int N = appWidgetIds.length; 

     // Perform this loop procedure for each App Widget that belongs to this provider 
     for (int i = 0; i < N; i++) { 
      // Here we setup the intent which points to the StackViewService which will 
      // provide the views for this collection. 
      final Intent intent = new Intent(context, ChecksWidgetService.class); 
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
      // When intents are compared, the extras are ignored, so we need to embed the extras 
      // into the data so that the extras will not be ignored. 
      intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
      final RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.checks_widget); 
      rv.setRemoteAdapter(android.R.id.list, intent); 

      // The empty view is displayed when the collection has no items. It should be a sibling 
      // of the collection view. 
      rv.setEmptyView(android.R.id.list, android.R.id.empty); 

      // Here we setup the a pending intent template. Individuals items of a collection 
      // cannot setup their own pending intents, instead, the collection as a whole can 
      // setup a pending intent template, and the individual items can set a fillInIntent 
      // to create unique before on an item to item basis. 
      final Intent toastIntent = new Intent(context, ChecksWidgetProvider.class); 
      toastIntent.setAction(ChecksWidgetProvider.TOAST_ACTION); 
      toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
      toastIntent.setData(Uri.parse(toastIntent.toUri(Intent.URI_INTENT_SCHEME))); 
      final PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      rv.setPendingIntentTemplate(android.R.id.list, toastPendingIntent); 

      appWidgetManager.updateAppWidget(appWidgetIds[i], rv); 
     } 
    } 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     final AppWidgetManager mgr = AppWidgetManager.getInstance(context); 
     if (intent.getAction().equals(TOAST_ACTION)) { 
      final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
      final long rowId = intent.getLongExtra("rowId", 0); 
      final int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0); 
      Toast.makeText(context, "Touched view " + viewIndex + " (rowId: " + rowId + ")", Toast.LENGTH_SHORT).show(); 
     } 
     super.onReceive(context, intent); 
    } 

    @Override 
    public void onAppWidgetOptionsChanged(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId, final Bundle newOptions) { 
     updateAppWidget(context, appWidgetManager, appWidgetId, newOptions.getLong("rowId")); 
    } 

    public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId, final long rowId) { 
     final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.checks_widget); 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
} 

यह मूलतः आधिकारिक दस्तावेज़ से एक कॉपी/पेस्ट है। हम यहां अपनी स्थिर विधि देख सकते हैं। आइए दिखाएं कि यह वास्तव में rowId का उपयोग करता है।

हम विकल्प बदलते समय ऐप विजेट अपडेट करने के लिए एक और असफल (नीचे देखें) प्रयास भी देख सकते हैं (onAppWidgetOptionsChanged)।

Service एक ऐप्स संकलनों के आधार पर विजेट के लिए आवश्यक लगभग एक सटीक प्रतिलिपि/doc का पेस्ट है:

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class ChecksWidgetService extends RemoteViewsService { 
    @Override 
    public RemoteViewsFactory onGetViewFactory(final Intent intent) { 
     return new StackRemoteViewsFactory(this.getApplicationContext(), intent); 
    } 
} 

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory { 
    private static final int mCount = 10; 
    private final List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>(); 
    private final Context mContext; 
    private final int mAppWidgetId; 
    private final long mRowId; 

    public StackRemoteViewsFactory(final Context context, final Intent intent) { 
     mContext = context; 
     mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
     mRowId = intent.getLongExtra("rowId", 0); 
    } 

    @Override 
    public void onCreate() { 
     // In onCreate() you setup any connections/cursors to your data source. Heavy lifting, 
     // for example downloading or creating content etc, should be deferred to onDataSetChanged() 
     // or getViewAt(). Taking more than 20 seconds in this call will result in an ANR. 
     for (int i = 0; i < mCount; i++) { 
      mWidgetItems.add(new WidgetItem(i + " (rowId: " + mRowId + ") !")); 
     } 

     // We sleep for 3 seconds here to show how the empty view appears in the interim. 
     // The empty view is set in the StackWidgetProvider and should be a sibling of the 
     // collection view. 
     try { 
      Thread.sleep(3000); 
     } catch (final InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     // In onDestroy() you should tear down anything that was setup for your data source, 
     // eg. cursors, connections, etc. 
     mWidgetItems.clear(); 
    } 

    @Override 
    public int getCount() { 
     return mCount; 
    } 

    @Override 
    public RemoteViews getViewAt(final int position) { 
     // position will always range from 0 to getCount() - 1. 

     // We construct a remote views item based on our widget item xml file, and set the 
     // text based on the position. 
     final RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item); 
     rv.setTextViewText(R.id.widget_item, mWidgetItems.get(position).text); 

     // Next, we set a fill-intent which will be used to fill-in the pending intent template 
     // which is set on the collection view in StackWidgetProvider. 
     final Bundle extras = new Bundle(); 
     extras.putInt(ChecksWidgetProvider.EXTRA_ITEM, position); 
     final Intent fillInIntent = new Intent(); 
     fillInIntent.putExtras(extras); 
     rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent); 

     // You can do heaving lifting in here, synchronously. For example, if you need to 
     // process an image, fetch something from the network, etc., it is ok to do it here, 
     // synchronously. A loading view will show up in lieu of the actual contents in the 
     // interim. 
     try { 
      L.d("Loading view " + position); 
      Thread.sleep(500); 
     } catch (final InterruptedException e) { 
      e.printStackTrace(); 
     } 

     // Return the remote views object. 
     return rv; 
    } 

    @Override 
    public RemoteViews getLoadingView() { 
     // You can create a custom loading view (for instance when getViewAt() is slow.) If you 
     // return null here, you will get the default loading view. 
     return null; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 1; 
    } 

    @Override 
    public long getItemId(final int position) { 
     return position; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public void onDataSetChanged() { 
     // This is triggered when you call AppWidgetManager notifyAppWidgetViewDataChanged 
     // on the collection view corresponding to this factory. You can do heaving lifting in 
     // here, synchronously. For example, if you need to process an image, fetch something 
     // from the network, etc., it is ok to do it here, synchronously. The widget will remain 
     // in its current state while work is being done here, so you don't need to worry about 
     // locking up the widget. 
    } 
} 

और आखिरी, मेरे विजेट लेआउट पर:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/widgetLayout" 
    android:orientation="vertical" 
    android:padding="@dimen/widget_margin" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/resizeable_widget_title" 
     style="@style/show_subTitle" 
     android:padding="2dp" 
     android:paddingLeft="5dp" 
     android:textColor="#FFFFFFFF" 
     android:background="@drawable/background_pink_striked_transparent" 
     android:text="@string/show_title_key_dates" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_marginRight="5dp" 
     android:layout_marginLeft="5dp" 
     android:background="@color/timeline_month_dark" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <TextView 
     android:id="@android:id/empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:textColor="#ffffff" 
     android:textStyle="bold" 
     android:text="@string/empty_view_text" 
     android:textSize="20sp" /> 

</LinearLayout> 

प्रासंगिक अनुभाग

<receiver android:name="com.my.full.pkg.ChecksWidgetProvider"> 
    <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    </intent-filter> 

    <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/checks_widget_info" /> 
</receiver> 
<activity android:name="com.my.full.pkg.ChecksWidgetConfigureActivity"> 
    <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> 
    </intent-filter> 
</activity> 
<service 
    android:name="com.my.full.pkg.ChecksWidgetService" 
    android:permission="android.permission.BIND_REMOTEVIEWS" /> 

: मेरे एंड्रॉयड प्रकट एक्सएमएल फ़ाइल की 0:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="146dp" 
    android:minHeight="146dp" 
    android:updatePeriodMillis="86400000" 
    android:initialLayout="@layout/checks_widget" 
    android:configure="com.my.full.pkg.ChecksWidgetConfigureActivity" 
    android:resizeMode="horizontal|vertical" 
    android:previewImage="@drawable/resizeable_widget_preview" /> 

तो, क्या गलत है? खैर, जब मैं विजेट बना देता हूं तो यह खाली होता है। मेरा मतलब शून्य है। खाली। कुछ भी तो नहीं। मेरे लेआउट में परिभाषित खाली दृश्य नहीं है! क्या बकवास है?

यदि मैं ऐप को पुनर्स्थापित करता हूं या डिवाइस को रीबूट करता हूं (या लॉन्चर ऐप को मारता हूं), ऐप विजेट वास्तव में अपडेट किया गया है और इसमें 10 आइटम शामिल हैं जो उदाहरण के रूप में स्वचालित रूप से जोड़े जाते हैं।

कॉन्फ़िगरेशन गतिविधि समाप्त होने के बाद मुझे अद्यतन करने के लिए हानिकारक चीज़ नहीं मिल सकती है। दस्तावेज़ से लिया गया यह वाक्य मेरे बाहर है: "ऐप विजेट बनाया गया है जब ऑनअपडेट() विधि को नहीं कहा जाएगा [...] - यह केवल पहली बार छोड़ा गया है।"।

मेरा प्रश्न है:

  • क्यों दुनिया में एंड्रॉयड देव टीम पहली बार विजेट बनाई गई है के लिए अद्यतन कॉल करने के लिए नहीं चुना है किया?
  • कॉन्फ़िगरेशन गतिविधि समाप्त होने से पहले मैं अपने ऐप विजेट को कैसे अपडेट कर सकता हूं?

    1. , संकलित पिछले कोड के साथ एप्लिकेशन इंस्टॉल करें लांचर पर अंतरिक्ष तैयार, लांचर
    2. से "विजेट" मेनू खोलने:

एक और बात यह है कि मुझे समझ नहीं आता कार्रवाई प्रवाह है

  • मेरी विजेट चुनें और वांछित क्षेत्र
  • उस पल में करने के लिए यह जगह, मेरे ऐप विजेट प्रदाता android.appwidget.action.APPWIDGET_ENABLED प्राप्त करता है और उसके बाद android.appwidget.action.APPWIDGET_UPDATE
  • तब मेरे एप्लिकेशन विजेट प्रदाता अपनेहो जाता हैविधि कहा जाता है। मुझे यह होने की उम्मीद है कि कॉन्फ़िगरेशन गतिविधि समाप्त होने के बाद ...
  • मेरी कॉन्फ़िगरेशन गतिविधि शुरू हो गई है। लेकिन ऐप विजेट पहले से ही बनाया और अपडेट किया गया है, जो मुझे समझ में नहीं आता है।
  • मैं अपने विन्यास गतिविधि से आइटम चुनें: onListItemClick कहा जाता हो जाता है
  • स्थिर updateAppWidget मेरी प्रदाता से कहा जाता है, सख्त विजेट अद्यतन करने के लिए कोशिश कर रहा।
  • कॉन्फ़िगरेशन गतिविधि इसके परिणाम और समाप्त करती है।
  • प्रदाता android.appwidget.action.APPWIDGET_UPDATE_OPTIONS प्राप्त करता है: ठीक है, जो बनाए जाने पर आकार अपडेट प्राप्त करने के लिए बहुत अधिक समझ में आता है। यही वह जगह है जहां मैं अपने प्रदाता से updateAppWidget
  • onUpdate पर कॉल नहीं किया जाता है। क्यूं कर??!!
  • अंत में: विजेट खाली है। सूचीदृश्य-खाली या @android: id/खाली खाली, वास्तव में EMPTY। कोई दृश्य प्रदर्शित नहीं किया गया। कुछ भी तो नहीं।
    यदि मैं ऐप को फिर से इंस्टॉल करता हूं, तो ऐप विजेट अपेक्षित के रूप में सूचीदृश्य के अंदर विचारों के साथ पॉप्युलेट किया जाता है।
    विजेट का आकार बदलने का कोई प्रभाव नहीं पड़ता है। यह सिर्फ onAppWidgetOptionsChanged को फिर से कॉल करता है, जिसका कोई प्रभाव नहीं पड़ता है।

    मेरा खाली मतलब क्या है: ऐप विजेट लेआउट फुलाया गया है, लेकिन सूचीदृश्य फुलाया नहीं गया है और खाली दृश्य प्रदर्शित नहीं होता है।

    +0

    मुझे आपकी विजेट कॉन्फ़िगरेशन xml और AndroidManifest.xml नहीं दिखाई देता है, क्या आप उन्हें भी प्रदान कर सकते हैं? –

    +0

    मैंने 'AndroidManifest.xml' प्रासंगिक अनुभाग जोड़ा है, मैं जल्द ही विजेट कॉन्फ़िगरेशन xml जोड़ दूंगा (यह अभी तक स्रोत भंडार पर नहीं है, इसलिए अब से कुछ घंटों तक मुझे स्रोत तक पहुंच नहीं होगी)। –

    उत्तर

    28

    AppWidgetManager के माध्यम से अद्यतन करने की कमी यह है कि आपको रिमोट व्यू प्रदान करना होगा - एक डिजाइन बिंदु से - यह समझ में नहीं आता है क्योंकि रिमोट व्यू से संबंधित तर्क को AppWidgetProvider (या में RemoteViewsService.RemoteViewsFactory में आपका मामला)।

    एक स्थिर विधि के माध्यम से RemoteViews तर्क का पर्दाफाश करने के SciencyGuy का दृष्टिकोण है कि से निपटने के लिए एक ही रास्ता है, लेकिन वहाँ एक और अधिक सुरुचिपूर्ण समाधान विजेट के लिए सीधे एक प्रसारण भेजने है:

    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, ChecksWidgetProvider.class); 
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {mAppWidgetId}); 
    sendBroadcast(intent); 
    

    एक परिणाम AppWidgetProvider के onUpdate (के रूप में) विजेट के लिए रिमोट व्यू बनाने के लिए विधि कहा जाएगा।

    +1

    यह सही जवाब होना चाहिए !! यह मेरा दिन बचाया;) – Mirko

    +0

    कृपया, स्वीकार किए गए एक से पहले इस उत्तर को आजमाएं, जिसने कुछ ऐप को कुछ अलग तर्कों के कारण कुछ बग में पेश किया। कॉन्फ़िगरेशन गतिविधि के बाद मैं यह तरीका उपयोग करता हूं, और यह बहुत अच्छा काम करता है। –

    +1

    एफवाईआई 'स्वीकार्य' उत्तर @ हेनरिक्यूसिया अपने लेखन जोनास के जवाब के समय था। मैंने अभी इमानुएल को स्वीकार किए जाने के रूप में चिह्नित किया है। –

    2

    मुझे आपके appwidgetprovider.xml और AndroidManifest.xml नहीं दिखाई दिए, लेकिन मेरा अनुमान है कि आपने अपनी कॉन्फ़िगरेशन गतिविधि को ठीक से सेट नहीं किया है।

    यहाँ यह कैसे करना है:

    1. अपने appwidgetprovider.xml को निम्नलिखित विशेषता जोड़ें:

      <activity android:name=".ChecksWidgetConfigureActivity"> 
          <intent-filter> 
           <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/> 
          </intent-filter> 
      </activity> 
      
    2. :

      <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
          ... 
          android:configure="com.full.package.name.ChecksWidgetConfigureActivity" 
          ... /> 
      
    3. आपका विन्यास गतिविधि एक उपयुक्त intent-filter होना चाहिए

    यदि कॉन्फ़िगरेशन गतिविधि सही तरीके से कॉन्फ़िगर की गई है, तो onUpdate() केवल इसे समाप्त होने के बाद ट्रिगर किया जाता है।

    +0

    अगर यह ठीक से कॉन्फ़िगर नहीं किया गया था, तो मेरी कॉन्फ़िगरेशन गतिविधि दिखाई नहीं देगी, है ना? मैंने अपनी मैनिफेस्ट प्रविष्टियों के साथ प्रश्न अपडेट किया है। –

    15

    आप सही हैं कि कॉन्फ़िगरेशन गतिविधि समाप्त होने के बाद ऑनअपडेट-विधि ट्रिगर नहीं होती है। प्रारंभिक अद्यतन करने के लिए यह आपकी कॉन्फ़िगरेशन गतिविधि पर निर्भर है। तो आपको प्रारंभिक दृश्य बनाना होगा।

    यह एक क्या विन्यास के अंत में क्या करना चाहिए का सार है:

    // First set result OK with appropriate widgetId 
    Intent resultValue = new Intent(); 
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    setResult(RESULT_OK, resultValue); 
    
    // Build/Update widget 
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); 
    
    // This is equivalent to your ChecksWidgetProvider.updateAppWidget()  
    appWidgetManager.updateAppWidget(appWidgetId, 
               ChecksWidgetProvider.buildRemoteViews(getApplicationContext(), 
                         appWidgetId)); 
    
    // Updates the collection view, not necessary the first time 
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.notes_list); 
    
    // Destroy activity 
    finish(); 
    

    आप पहले से ही परिणाम को ठीक से सेट। और आप ChecksWidgetProvider.updateAppWidget() को कॉल करते हैं, हालांकि UpdateAppWidget() सही परिणाम नहीं देता है।

    अद्यतन ऐपविड्जेट() वर्तमान में एक खाली रिमोट व्यू-ऑब्जेक्ट देता है। जो बताता है कि आपका विजेट पहले क्यों खाली है। आपने कुछ भी नहीं देखा है। मेरा सुझाव है कि आप() विधि है जो आप दोनों onUpdate और updateAppWidget (से कॉल कर सकते हैं) एक स्थिर buildRemoteViews को onUpdate से अपने कोड के लिए कदम:

    public static RemoteViews buildRemoteViews(final Context context, final int appWidgetId) { 
         final RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.checks_widget); 
         rv.setRemoteAdapter(android.R.id.list, intent); 
    
         // The empty view is displayed when the collection has no items. It should be a sibling 
         // of the collection view. 
         rv.setEmptyView(android.R.id.list, android.R.id.empty); 
    
         // Here we setup the a pending intent template. Individuals items of a collection 
         // cannot setup their own pending intents, instead, the collection as a whole can 
         // setup a pending intent template, and the individual items can set a fillInIntent 
         // to create unique before on an item to item basis. 
         final Intent toastIntent = new Intent(context, ChecksWidgetProvider.class); 
         toastIntent.setAction(ChecksWidgetProvider.TOAST_ACTION); 
         toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
         toastIntent.setData(Uri.parse(toastIntent.toUri(Intent.URI_INTENT_SCHEME))); 
         final PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
         rv.setPendingIntentTemplate(android.R.id.list, toastPendingIntent); 
    
         return rv; 
    } 
    
    public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) { 
        final RemoteViews views = buildRemoteViews(context, appWidgetId); 
        appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
    
    @Override 
    public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) { 
        super.onUpdate(context, appWidgetManager, appWidgetIds); 
    
        // Perform this loop procedure for each App Widget that belongs to this provider 
        for (int appWidgetId: appWidgetIds) { 
         RemoteViews rv = buildRemoteViews(context, appWidgetId); 
         appWidgetManager.updateAppWidget(appWidgetIds[i], rv); 
        } 
    } 
    

    कि विजेट आरंभीकरण का ध्यान रखना चाहिए।

    मेरे नमूना कोड में फिनिश() को कॉल करने से पहले अंतिम चरण संग्रह दृश्य अपडेट कर रहा है। जैसा कि टिप्पणी कहती है, यह पहली बार जरूरी नहीं है। हालांकि, मैं इसे शामिल करता हूं यदि आप किसी विजेट को जोड़े जाने के बाद पुनः कॉन्फ़िगर करने की अनुमति देना चाहते हैं। उस स्थिति में, उचित दृश्य और डेटा लोड होने के लिए, मैन्युअल रूप से संग्रह दृश्य को अपडेट करना होगा।

    +0

    वह महान था! प्रतिभाशाली! मुझे लगता है कि मैं डॉक्टर के साथ थोड़ा सा खो गया था। एक बार फिर धन्यवाद। –

    संबंधित मुद्दे

     संबंधित मुद्दे