2010-08-25 8 views
9

अपडेट को मजबूर करने के लिए विजेट क्लास पर APPWIDGET_UPDATE इरादा को आग लगाने के लिए मेरे विजेट में एक बटन चाहिए, लेकिन मुझे इरादे में स्थिर फ़ील्ड के रूप में APPWIDGET_UPDATE दिखाई नहीं दे रहा है।क्या APPWIDGET_UPDATE प्रोग्रामेटिक रूप से एक इरादा फेंकना संभव है?

क्या यह संभव है, और यह कैसे करेगा?

Intent intent = new Intent(context, BaseWidgetProvider.class); 
intent.setAction({APPWIDGET_UPDATE INTENT HERE}) 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

views.setOnClickPendingIntent(R.id.MyWidgetButton, pendingIntent); 

उत्तर

15

हाँ, यह संभव है। आप AppWidgetManager में कार्रवाई मिल जाएगा:

intent.setAction (AppWidgetManager.ACTION_APPWIDGET_UPDATE)

संपादित करें: आप विगेट्स आप अपडेट करना चाहते का आईडी प्रदान करने के लिए की आवश्यकता होगी। नीचे एक पूर्ण नमूना है।

AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 
ComponentName widgetComponent = new ComponentName(context, YourWidget.class); 
int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent); 
Intent update = new Intent(); 
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds); 
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
context.sendBroadcast(update); 
+0

धन्यवाद। मैं एडीबी लॉगकैट में इरादा आग देखता हूं, लेकिन विजेट अपडेट नहीं होता है। मुझे और कुछ करना चाहिए? मेरे पास पहले से ही मेरी मेनिफेस्ट फ़ाइल में एक इरादा फ़िल्टर के रूप में APPWIDGET_UPDATE है। – NPike

+0

'onUpdate()' नहीं कहा जाता है? मुझे उस पर वापस आना होगा। – Justin

+0

आपको अपडेट करने के लिए विजेट्स की आईडी प्रदान करने की आवश्यकता होगी। यदि आप अपने विजेट के सभी उदाहरण अपडेट करना चाहते हैं, तो आप 'AppWidgetManager' से आईडी की पूरी सूची प्राप्त कर सकते हैं (मैंने कोड को शामिल करने के लिए अपनी टिप्पणी संपादित की है)। – Justin

2

मैं जानता हूँ कि यह एक बहुत पुरानी सवाल है, लेकिन मुझे लगता है कि यह दिलचस्प हो सकता है, क्योंकि एंड्रॉयड अद्यतन AppWidgets नीतियों को ताज़ा करें। मुझे लगता है कि यह परिवर्तन अपेक्षित उत्तर को उम्मीद के अनुसार काम करने से रोक सकता है।

यह मेरा समाधान है, RemoteViews और एक संग्रह का उपयोग कर।

public static final String ACTION_WIDGET_UPDATE = "com.yourpackage.widget.ACTION_UPDATE"; 

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(ACTION_WIDGET_UPDATE)) { 
     int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0); 
     AppWidgetManager.getInstance(context) 
       .notifyAppWidgetViewDataChanged(widgetId, R.id.widgetColectionRoot); 
    } 
    super.onReceive(context, intent); 
} 

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
        int[] appWidgetIds) { 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
    for (int widgetId : appWidgetIds) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      RemoteViews collectionRemoteView = getRemoteViews(widgetId, context); 
      appWidgetManager.updateAppWidget(widgetId, collectionRemoteView); 
     } 

    } 
} 

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
@SuppressWarnings("deprecation") 
private RemoteViews getRemoteViews(int widgetId, Context context) { 
    // Sets up the intent that points to the RemoteViewService 
    // that will 
    // provide the views for this collection. 
    Intent widgetUpdateServiceIntent = new Intent(context, 
      RemoteViewsService.class); 
    widgetUpdateServiceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); 
    // 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. 
    widgetUpdateServiceIntent.setData(
      Uri.parse(widgetUpdateServiceIntent.toUri(Intent.URI_INTENT_SCHEME))); 
    RemoteViews collectionRemoteView = new RemoteViews(context.getPackageName(), 
      R.layout.widget_collection); 
    collectionRemoteView.setRemoteAdapter(widgetId, 
      R.id.widgetColectionRoot, widgetUpdateServiceIntent); 

    collectionRemoteView.setEmptyView(R.id.widgetColectionRoot, R.id.widgetEmpty); 

    // This section makes it possible for items to have 
    // individualized behavior. 
    // It does this by setting up a pending intent template. 
    // Individuals items of a collection 
    // cannot set up their own pending intents. Instead, the 
    // collection as a whole sets 
    // up a pending intent template, and the individual items set a 
    // fillInIntent 
    // to create unique behavior on an item-by-item basis. 
    Intent selectItemIntent = new Intent(context, 
      BrochuresWidgetProvider.class); 

    Intent refreshIntent = new Intent(selectItemIntent); 
    refreshIntent.setAction(ACTION_WIDGET_UPDATE); 
    PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
      context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    collectionRemoteView.setOnClickPendingIntent(R.id.widgetReload, 
      refreshPendingIntent); 
    return collectionRemoteView; 
} 

बेशक, आपको अपने विजेट प्रदाता घोषणा के अंदर, अपने मैनिफेस्ट पर उस इरादे-फ़िल्टर को पंजीकृत करने की भी आवश्यकता है।

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