Reputation: 105
i want to add some countdown timer to my activity, e.g. there will be a one minute timer and during that time a user is asked to memorize certain pictures. after one minute is passed (from that activity), a pop up message will show that the user has to go to the next screen. is there anyone who can help me out? thanks
Upvotes: 1
Views: 1704
Reputation: 5535
Here is the exmaple
create a object of the class given below like this
final CounterClass timer = new CounterClass(180000,1000); // 1800000 and 1000 are in milli seconds
here 180000 is equal to 3 minutes
180000/1000
= 180 seconds and 180/60 = 3 minutes
and 1000 in CounterClass(180000,1000);
means interval between the next tick
and for 1 Minute use 60000 instead of 1800000
Start the timer
timer.start();
end the timer
timer.cancel();
here is the counter class
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override // when timer is finished
public void onFinish() {
System.out.println("Completed.");
// here start the new activity
}
@Override // on every tick of the timer
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
}
}
}
Upvotes: 2
Reputation: 1413
try the timer thread and handle thread,
final Handler h = new Handler();
final Runnable animateViewPager = new Runnable()
{
public void run()
{
imageview.setBAckgroundresource(imageView[postion));
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
h.post(animateViewPager);
}
},1000,60*1000);
Upvotes: 1
Reputation: 666
use timer method as follows,
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//run your code here
}
},0,60*1000);
// here 60 seconds * 1000 milliseconds
// finally don't forget to cancel the timer
timer.cancel();
It makes your code run for 1 minute then it moves to next block there are other approaches available check the Android documentation
or you can make use of following code,
long start = System.currentTimeMillis();
long end = start + 60 * 1000;
while (System.currentTimeMillis() < end) {
// do your stuff here
}
Upvotes: 2
Reputation: 11194
Try this code snippet:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
new AlertDialog.Builder(this)
.setTitle("Navigate")
.setMessage("Next Activity?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
// start intent to next activity
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}.start();
Upvotes: 1