What is the right way to make a widget that changes content every 6-8 seconds, Android

The closest example of what I want to do is the Engadget widget. It updates data from the Internet every 5-10 minutes and "scrolls" to stories every 5-7 seconds. I guess it sets an interval of 5-10 minutes for widget providercalling onUpdate without inbuild restrictions, something like this.

AlarmManager alarms = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRateSeconds * 1000, newPending); 

Then the real problem .. to update the contents of widgets without calling onUpdate.

Here, I depict that inside the widget provider there is a timer or Runnable, which every time it is called, it overwrites itself with something like this

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 = SystemClock.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 the widget provider inside onDeletedandonDisabled

i removeCallbacks mHandler.removeCallbacks(mUpdateTimeTask);

- ? AlarmManager ? - onUpdate, runnable .

+3
2
+1
+1

Source: https://habr.com/ru/post/1766288/


All Articles