简体   繁体   中英

when restart CountDownTimer, it finish work

I want to stop CountDownTimer and to restart it, with time when it stopped. I have this code in class CountDownTimer

public class MyDownTimer extends CountDownTimer{

long mills;
public MyDownTimer(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);

    this.mills=millisInFuture;
}

@Override
public void onFinish() {

    GameScreen.showWindow("Time is over","Sorry, time is Over, you lose");
}

@Override
public void onTick(long millisUntilFinished) {

    mills=millisUntilFinished/1000;
    GameManager.time= millisUntilFinished/1000;
}       
}

and Dialog class, he must to showing, when user click in button 'pause'. Dialog code, as you can see, i start new CountDownTimer in this code

 public static void showWindowPause(final MyDownTimer dTime){

    final Dialog wdialog= new Dialog(context);
    wdialog.setContentView(R.layout.dialog_window);
    wdialog.setTitle("title");
    TextView text=(TextView)wdialog.findViewById(R.id.txtFirstWord);
    text.setText("mainText");
    Button dialogButton=(Button)wdialog.findViewById(R.id.button1);
    dialogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            wdialog.dismiss();              
            dTime.start();


        }
    });
    wdialog.show();

}

and method, where I stoping CountDownTimer and show Dialog window.

long stime =dTime.mills;
dTime.cancel();
dTime=null;
dTime= new MyDownTimer(stime,1000);
GameScreen.showWindowPause(dTime);

when i click button in Dialog window, i see new Dialog, which created in method

MyDownTimer.onFinish()

But if I change line

long stime =dTime.mills;

on

long stime =5000;

CountDownTimer restarts,and all normal. where i made mistake?

Your question is really confusing , but I THINK I found something that doesn't make sense.

public void onTick(long millisUntilFinished) {
    mills=millisUntilFinished/1000;     
    GameManager.time= millisUntilFinished/1000;
} 

I think it should be

mills=millisUntilFinished - 1000;

edit: If you are actually converting millseconds to seconds, then you'll need to do

long stime =dTime.mills*1000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM