0

I know this has been discussed many times, but it's still not clear to me how to implement this properly. I want to update a widget more often than every 30 minutes (the minimum of the updatePeriodMillis attribute).

I know I should use AlarmManager for this, but all examples I've seen use the AlarmManager to call a Service that updates the widget. Why can it not call the AppWidgetProvider's onUpdate() method? If a second class is used for this why should I still have an (empty) AppWidgetProvider? Why use a Service (which runs in the background I presume) if the AlarmManager calls it repeating anyway? Why not use a BroadcastReceiver?

0ne_Up
  • 471
  • 3
  • 9
  • 24
  • I think the concept is to use an alarm manager, to send out an update broadcast. Since you can have multiple widgets of the same time.. they all recieve the broadcast, acknowledge they need to update, and then do update themselves. (I do not use a service) – IAmGroot May 20 '16 at 09:02
  • You can try [this](https://stackoverflow.com/a/60275428/8956604). It works fine. – Kasım Özdemir Feb 18 '20 at 07:14

1 Answers1

0

No need to use AlarmManager for widget updates because this characteristics is already in AppWidgetProvider class.

Extends AppWidgetProvider class in your class :

public class MyClass extends AppWidgetProvider

now override onUpdate method (to get update notification)

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

Now set AppWidgetHost in your main class and AppWidgetManager (from where send updates to app widget)

mAppWidgetManager = AppWidgetManager.getInstance(this);    
mAppWidgetHost = new AppWidgetHost(this, APP_WIDGET_HOST_ID);

Now update the widget

mAppWidgetManager.updateAppWidget(appWidgetId, mRemoteView);
  • This will not update the widget more often than every 30 minutes. This is the minimum of the updatePeriodMillis attribute. I want to update it every minute or so. – 0ne_Up May 20 '16 at 09:55