I use CountDownTimer in my code and when i run app i have one problem that is when i go to next level ( go to next activity ) the timer should be stop and dont run the onfinish method but when the first level timer down it run the onfinish method and go to activity GameOver .
I use Intent to move between my activity and my CountDownTimer is :
new CountDownTimer(timeoflevel, 1000) {
public void onTick(long millisUntilFinished) {
txtclock2.setText("Time: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
txtclock2.setText("..Finish..");
Intent intent = new Intent(Play.this , GameOver.class);
startActivity(intent);
}
}.start();
Sorry for my easy question and thanks for reading .
CountDownTimer countDownTimer;
countDownTimer = new CountDownTimer(timeoflevel, 1000) {
public void onTick(long millisUntilFinished) {
txtclock2.setText("Time: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
txtclock2.setText("..Finish..");
Intent intent = new Intent(Play.this , GameOver.class);
startActivity(intent);
}
}.start();
// and in OnStop
public void onStop(){
countDownTimer.cancel();
}
You should call countDownTimer.cancel() before starting next level. It will cancel the counDownTimer and will not call onFinish.
public void onStop(){
//Cancel your timer here before going to next activity.
}
CountDownTimer countDownTimer;
//in on Resume()
countDownTimer = new CountDownTimer(timeoflevel, 1000) {
public void onTick(long millisUntilFinished) {
txtclock2.setText("Time: " + millisUntilFinished / 1000);
//TODO perform operation
}
public void onFinish() {
txtclock2.setText("..Finish..");
Intent intent = new Intent(Play.this , GameOver.class);
startActivity(intent);
}
}.start();
// Cancel Timer in onPause()
public void in onPause(){
countDownTimer.cancel();
}
Related
where should I edit this code, that i can stop the CountDown ?
How to delete the old one before I start the next new CountDown ?
public void MyCounter1(){
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
}.start();
}
here is code:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
};
timer.start();
}
// To stop & start new timer check not null of timer instance first then cancel existing timer & start new one
if(timer != null){
timer.cancel();
MyCounter1();
}
also if you want to cancel first instance and start new one you can add above lines in onFinish method which will be get called when timer finish his time.
check this:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
if(timer != null){
timer.cancel();
MyCounter1();
}
}
};
timer.start();
}
I did these 2 lines. I works at the time fine:
public void MyCounter1(){
CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
if(my_condition=1){ //1- just make my_condition<>1 to stop the counter
txt_timer.setText("Left time : " + millisUntilFinished / 1000); }
}
public void onFinish() {
countDownTimer.cacel(); // 2- delete old one
txt_timer.setText("done");
}
}.start();
}
private int time = 10; //example 10 min.
CountDownTimer(60000 * time, 1000)
#Override
public void onFinish() {
txt_timer.setText("done");
cancel();
time = 0;
I want to make a method (service, alarm, etc.) that can be calculated after x downtime user with the app
Which closes the current activity
and will send the initial activity (login)
Thank you very much
http://androidbite.blogspot.in/2012/11/android-count-down-timer-example.html
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Refer this link and some examples on countdown timer if you want to use this.
I answer
code is
private long startTime=15*60*1000; // 15 MINS IDLE TIME
private final long interval = 1 * 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTimer = new MyCountDownTimer(startTime, interval);
}
#Override
public void onUserInteraction(){
super.onUserInteraction();
//Reset the timer on user interaction...
countDownTimer.cancel();
countDownTimer.start();
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
//DO WHATEVER YOU WANT HERE
// CIERRA LA APP MATANDO EL PROCESO Y VUELVE A ABRIRLO.
android.os.Process.killProcess(android.os.Process.myPid());
}
#Override
public void onTick(long millisUntilFinished) {
}
}
use
act.finishAffinity();
act.startActivity(new Intent(act, actMain.class));
In my activity i want to check if countdowntimer is finished i'll stop the activity and pass to another activity?How can i do this?
I define countdowntimer like this
mcountdowntimer = new CountDownTimer(25000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i);
}
#Override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
}
};
mcountdowntimer.start();
mProgressBar.setProgressTintList(ColorStateList.valueOf(Color.rgb(64,91,164)));
}
Just simply add that logic in your onFinish() method:
#Override
public void onFinish() {
//here add the code for starting a new activity
}
I have an Activity. All I want is if user don't touch screen, after a TIMEOUT, this activity will automatically finish(). Anyone have any ideas?
CountDownTimer countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//TODO: Do something every second
}
public void onFinish() {
finish();
//YourActivity.finish(); outside the actvitiy
}
}.start();
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
countDownTimer.cancel();
countDownTimer.start();
}
return super.onTouchEvent(event);
}
So basically you start a countDown timer and than wait for it to get completed. Once completed, call finish on the activity.
If in between the user touches the screen, you cancel the timer and start it again.
Don't forget to accept the answer :)
Look for how to wait/sleep in Android, examples:
wait for 3 seconds or user click
Wait a time - Android
How to "wait" a Thread in Android
And when the wait/sleep ends just call the finish() method.
Try this in your Activity:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
finish();
//YourActivity.finish(); outside the actvitiy
}
}.start();
http://developer.android.com/reference/android/os/CountDownTimer.html
use this :
int time =0;
then add onTouchListener for your root layout and update time value like this :
time = 0;
and use the handler for count like this :
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
time++;
if(time>=10000){
finish();
}
}
}, 1000);
it is clear i set finish time for 10 second.
I have a timer running for my IQ activity, and I want to Pause that timer when user click on Answer Button.
Bellow is my running timer and I have cancel timer when onlick but it's doesn't work for me:
timer = (TextView) findviewbyid(r.id.time)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
banana.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
CountDownTimer.cancel();
}
}
You can create a variable 'total' for total time and just set: total = millisUntilFinished in the onTick method. Check this answer: https://stackoverflow.com/a/6469166/2126403