The closest example of what i want to accomplish is the "Engadget" widget.
It updates it's data from the internet every 5-10 minutes and "scrolls" to the stories every 5-7 seconds.
I imagine it sets the 5 -10 minutes interval to the widget's provider
to call the onUpdate without the inbuild limitations , something like that ..
AlarmManager alarms = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRateSeconds * 1000, newPending);
Then the real issue.. to update the widgets content without calling onUpdate.
Here i imaging that inside the widget provider exists a timer or a Runnable that everytime it get's called it reschedules itself with something like that
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
...
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 1000);
}
.
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long millis = System开发者_如何学CClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
...
mHandler.postAtTime(this,start + (((minutes * 60) + seconds + 1) * 1000));
}
Then on widget provider inside
onDeleted
and onDisabled
i removeCallbacks mHandler.removeCallbacks(mUpdateTimeTask);
Is there anything wrong with that scheme?
Do i need to make a service to update the widget from there and keep it alive without setting an AlarmManager
to the widget's configuration?
For some reason i get double calls on onUpdate
and the runnable keeps running after i delete the widget.
Have a look at ViewFlipper
. You can use it to update the data once for a set of items and then set how frequently they cycle.
Anything that runs continuously (I.E. mp3 player or radio) or requires some form of polling (alarm, SO display widget, Weather bug) should be written as a service. I know this is old but check this these vids for more info on best pratices
http://developer.android.com/videos/index.html#v=M1ZBjlCRfz0
精彩评论