Answer the question
In order to leave comments, you need to log in
How to update a widget in Android at a given time using AlarmManager?
I have a widget that needs to be updated every day at a given time. I tried to implement this via AlarmManager but the widget is not updating.
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (ACTION_AUTO_UPDATE_WIDGET.equals(intent.getAction())) {
onUpdate(context, AppWidgetManager.getInstance(context),
AppWidgetManager.getInstance(context).getAppWidgetIds(newComponentName(context, Widget.class)));
}
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Intent intent = new Intent(Widget.ACTION_AUTO_UPDATE_WIDGET);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(PendingIntent.getBroadcast(context, 0, intent, 0));
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent intent = new Intent(Widget.ACTION_AUTO_UPDATE_WIDGET);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 17);
c.set(Calendar.MINUTE, 32);
c.set(Calendar.SECOND, 0);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int id : appWidgetIds) {
updateWidget(context, appWidgetManager, id);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question