How To Update Widget Every Minute
Solution 1:
Rather than using a thread in the AppWidget, you would be better served by using the AlarmManager to schedule a repeating AppWidget Update Intent which your code would handle appropriately.
The benefits of this approach is the possibility to configure the update rate, and also handle the case of the device sleeping (and not waking up to run your code, or even being blocked from sleeping because your thread is busy).
There are numerous examples around the internet that should explain the ins and outs of using the AlarmManager to raise your AppWidget Update Intents.
Solution 2:
The system sends a broadcast event at the exact beginning of every minutes based on system clock. Create a service with your widget and do something like this :
BroadcastReceiver _broadcastReceiver;
private final SimpleDateFormat _sdfWatchTime = newSimpleDateFormat("HH:mm");
privateTextView _tvTime;
@OverridepublicvoidonStart() {
super.onStart();
_broadcastReceiver = newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context ctx, Intent intent) {
if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
_tvTime.setText(_sdfWatchTime.format(newDate()));
}
};
registerReceiver(_broadcastReceiver, newIntentFilter(Intent.ACTION_TIME_TICK));
}
@OverridepublicvoidonStop() {
super.onStop();
if (_broadcastReceiver != null)
unregisterReceiver(_broadcastReceiver);
}
Don't forget however to initialize your TextView beforehand (to current system time) since it is likely you will pop your UI in the middle of a minute and the TextView won't be updated until the next minute happens.
Post a Comment for "How To Update Widget Every Minute"