user1076637
user1076637

Reputation: 768

How to update widgets of different sizes?

My app has supports multiple size widgets per the scheme outlined here how to add multiple widgets in one app?

It has a single abstract BaseWidgetProvider class that handle all the widgets. It is then subclassed by empty WidgetProvider1/2/3/4 classes (one per size) that are listed in the Android Manifest and everything works just fine. The registration in the manifest looks like this (typical for 1..4)

<receiver
    android:name=".widget.MyWidgetProvider4"
    android:label="My Widget size 4" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <intent-filter>
        <action  
            android:name="my.domain.BaseWidgetProvider.UPDATE_WIDGET" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/my_widget_info4" />
    </receiver>

My question is about sending an intent from the app to update the widgets with new data. I could not find a way to send a single intent for all 4 providers and am currently sending 4 seperate intents each time

for (Class cls : BaseWidgetProvider.WIDGET_PROVIDER_CLASSES) {
   Intent intent = new Intent(mApp.context(), cls);
   intent.setAction(BaseWidgetProvider.UPDATE_WIDGET);
       this.sendBroadcast(intent);
}

where

public static final Class[] WIDGET_PROVIDER_CLASSES = new Class[] {
    WidgetProvider4.class,
    WidgetProvider1.class,
    WidgetProvider2.class,
    WidgetProvider3.class      
};
  1. Is this the best way to send an update intent to all widget sizes (note that typically the user has only zero or one widgets)?

  2. Is there a reasonable way to send only a single intent?

  3. Is there a way to have a single receiver that will update widget instances of all four sizes?

Note that the base provider class does not care about the widget size and does exactly the same for all sizes. The only difference is the initial widget size on the user's screen. If the user resizes the widget (e.g. using Launcher Pro), it works just fine.

Upvotes: 1

Views: 818

Answers (1)

avepr
avepr

Reputation: 1896

Add an intent filter with one intent action into the declarations of all of your widget providers in Manifest file, e.g.:

   <intent-filter>
        <action android:name="my.domain.blablabla.MY_SUPER_INTENT_TO_UPDATE_ALL" />
    </intent-filter>

And then just send this intent from your application as broadcast. In this case the intent will be handled by all your widget providers.

Upvotes: 3

Related Questions