Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an update broadcast to an app widget

I have an app widget with a configure activity, and I want to trigger an update to the widget when an OK button in the activity is clicked. I wrote this code:

            Intent initialUpdateIntent=new Intent(AppWidgetManager.
                    ACTION_APPWIDGET_UPDATE);
            initialUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    widgetID);
            sendBroadcast(initialUpdateIntent);

But for some reason the onUpdate function is not called! Does anyone know what the problem might be? Thanks.

like image 757
user940016 Avatar asked May 19 '12 09:05

user940016


People also ask

How do I broadcast an app widget in Android?

You must declare your AppWidgetProvider class implementation as a broadcast receiver using the <receiver> element in the AndroidManifest (see Declaring an App Widget in the Manifest above). The AppWidgetProvider class extends BroadcastReceiver as a convenience class to handle the App Widget broadcasts.

What event broadcasts does the appwidgetprovider receive?

The AppWidgetProvider receives only the event broadcasts that are relevant to the App Widget, such as when the App Widget is updated, deleted, enabled, and disabled. When these broadcast events occur, the AppWidgetProvider receives the following method calls:

How to send a broadcast in Android with a package?

In Android 4.0 and higher, you can specify a package with setPackage(String) when sending a broadcast. The system restricts the broadcast to the set of apps that match the package. You can send local broadcasts with LocalBroadcastManager.

How do I register the connectivity_action broadcast on Android?

Also, apps targeting Android 7.0 and higher must register the CONNECTIVITY_ACTION broadcast using registerReceiver (BroadcastReceiver, IntentFilter) . Declaring a receiver in the manifest doesn't work. Apps can receive broadcasts in two ways: through manifest-declared receivers and context-registered receivers.


2 Answers

Try the following:

public static void updateWidgets(Context context) {
    Intent intent = new Intent(context.getApplicationContext(), DayActivitiesAppWidget.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
    // since it seems the onUpdate() is only fired on that:
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
    int[] ids = widgetManager.getAppWidgetIds(new ComponentName(context, DayActivitiesAppWidget.class));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        widgetManager.notifyAppWidgetViewDataChanged(ids, android.R.id.list);
    }

    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    context.sendBroadcast(intent);
}

Replace DayActivitiesAppWidget with your AppWidgetProvider class this way u update all your widget instances, then you can track down the issue with updating a single widget instance.

like image 125
forcewill Avatar answered Oct 09 '22 19:10

forcewill


Do you try to to catch your broastcast message in onReceive:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    String strAction = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(strAction)) {
        /* Do update */
    }
}

Otherwise, IMO it is better to define your own Message rather than using ACTION_APPWIDGET_UPDATE to clearly logic of update view of widget, something like that, in 1.Declare intent name in Manifest file:

<action android:name="com.yourdomain.youapp.SETTING_UPDATE" />

2.Define intent name:

public static final String SETTING_UPDATE = "com.yourdomain.youapp.SETTING_UPDATE";

3.Handle in onReceive:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    String strAction = intent.getAction();
    if (SETTING_UPDATE.equals(strAction)) {
        /* Do update setting */
    }
}

Hope it help ^^

like image 28
NguyenDat Avatar answered Oct 09 '22 19:10

NguyenDat