Update widget on day change
I have a widget that displays the current date among other things. However the widget only gets updated when a user performs an action in the main activity.
How would I go about forcing the widget to update the next time the phone is turned on after midnight to reflect the new date?
I understand I may need an AlarmManager but I'm n开发者_StackOverflow中文版ot clear on the parameters. And do I put it in my main activity and have it call the method that calls update on my widget?
in a similar style, I have a service that I want to run at a certain time. when this service has done it's work it then updates my widget and stops itself (the service). here is a snippet of how I launch my service at a specified time
Intent mSrvcIntent = new Intent(packageContext, MyService.class);
PendingIntent mSrvcPendingingIntent = PendingIntent.getService(packageContext,0, mSrvcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager mAlarmManager = (AlarmManager)context.getSystemService(android.content.Context.ALARM_SERVICE);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time to start service, mSrvcPendingingIntent);
I hope this is near enough for you to be able to solve your problem
ad this to your manifest widget receiver
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.intent.action.DATE_CHANGED" />...
onReceive() will fire
This worked for me:
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
精彩评论