How to update a widget periodically, say after every 5 seconds

10,672

Solution 1

First, I would strongly recommend you not to update a widget every 5 seconds. It would kill your battery in no time.

You can use android:updatePeriodMillis attribute in the appwidget-provider.
Take a look at Adding the AppWidgetProviderInfo Metadata on Android developer website.
The thing is, to preserve battery, you can not set a period under 30 min (1800000ms).

After setting up the update period you want, you just have to define the behavior in the onReceive() method of your AppWidgetProvider. To do this, you have to catch ACTION_APPWIDGET_UPDATE event.

@Override
public void onReceive(Context context, Intent intent) {

    final String action = intent.getAction();

    if (ACTION_APPWIDGET_UPDATE.equals(action)) {
        // Update your widget here.
    }
}

If you really want to perform a task every 5 seconds, you can use Timer and TimerTask class :

final Handler handler = new Handler();
Timer timer = new Timer();

TimerTask task = new TimerTask() {

    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {  
                // send a broadcast to the widget.
            }
        });
    }
};
timer.scheduleAtFixedRate(task, 0, 5000); // Executes the task every 5 seconds.

Solution 2

Use AlarmManager to tirgger off alarms that would send out an Update intent to your receiver.

Here's a good link which gives an example.

http://www.parallelrealities.co.uk/2011/09/using-alarmmanager-for-updating-android.html

  1. When the widget is activated, in the widget service, setup the next alarm after 5 seconds.
  2. The alarm should send out a PendingIntent, that would trigger your service after 5 seconds.
  3. In your service's onStartCommand, trigger the widget update service.
  4. And setup the next alarm after 5 seconds again.

Note: But, 5 seconds, is really too fast. It would drain off your battery soon enough, depending on what else you might be doing in the background. Do think about making the updates less frequently.

Solution 3

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        runOnUiThread(new Runnable() {
            public void run() {
                imageView.setImageBitmap(bitmap);
            }
        });
    }
}, 5000, 5000);

The first change will happen after 5 seconds.

Share:
10,672

Related videos on Youtube

Atihska
Author by

Atihska

I like learning new stuff and experimenting it. I really like stackoverflow as it helps me learn better and grow.

Updated on September 16, 2022

Comments

  • Atihska
    Atihska over 1 year

    I have a widget with simple button implementation, that whenever we click on a button it flips through a given set of images. Now if I want to flip it every 5 seconds without the button being clicked, how may I proceed?

  • Atihska
    Atihska almost 11 years
    Where should I put this code as in in Provider or Service class of android-widget?
  • Richard Barraclough
    Richard Barraclough almost 4 years
    Where do you put this?
  • Richard Barraclough
    Richard Barraclough almost 4 years
    And where does bitmap come from?
  • Richard Barraclough
    Richard Barraclough almost 4 years
    But there isn't a Fragment or an Activity because it's a widget.
  • Richard Barraclough
    Richard Barraclough almost 4 years
    And you can't start the timer in onReceive because that will start a new timer every time the OS sends the update request to the widget.
  • Richard Barraclough
    Richard Barraclough almost 4 years
    What widget service? Send out how? I can't find an onStartCommand function.
  • Shubham Gupta
    Shubham Gupta over 2 years
    runOnUiThread needs activity refrence, Where do I put all these code?