Reputation: 695
So I am trying to have a picture automatically refresh every 2 seconds. I've tried the handler/timer method, but I'm confused as how to call it and for some reason can not get it to actually refresh...
private void refreshPicture(final String refreshRate, final String userName, final String userPicture) {
final Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
long millis = Long.parseLong(refreshRate);
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
//my method where i refresh image
getUserPicture(userName, userPicture);
timerHandler.postDelayed(this, 500);
}
};
}
Upvotes: 0
Views: 161
Reputation: 1090
You forgot to initiate the first call of the runnable. After defining the timerRunnable
, you have to call timerHandler.post(timerRunnable);
once. Also, if you want to have it refreshed every 2 seconds, you have to put 2000 instead of 500 as value in postDelayed()
.
Don’t forget to call timerHandler.removeCallbacks(timerRunnable)
when you want to stop refreshing.
Upvotes: 3