您好我正在尝试暂停并恢复我的CountDownTimer但它不起作用,首先我将millisUntilFinished
变量设为futureMins
并且在我设置的事件监听器的onClick()
方法中设置futureMins
到mins
我想在我调用countDownTimer.start();
之后将这些变量设置为彼此时,CountDownTimer将从最后一次开始,但它不会从上次开始:(
修改:I solved problem with using this class
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TimeActivity extends AppCompatActivity {
String output;
long seconds;
private long futureMins;
private long mins = 2401000 ;
private TextView secText;
TimeOutReminderService service;
public boolean onBreak=false;
Snackbar snackbar;
CountDownTimer countDownTimer;
CountDownTimer countDownTimer2;
public TextView minText;
public Button pauseButton;
private Button continueButton;
private Button silenceButton;
boolean cameFromSilenceButtonClick=false;
@Override
protected void onPause() {
Log.i("INFO","onPause");
super.onPause();
}
@Override
protected void onStop() {
Log.i("INFO","onStop");
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.ZaaTheme);
setContentView(R.layout.time_activity);
final View view=findViewById(android.R.id.content);
pauseButton=(Button)findViewById(R.id.pauseButton);
minText=(TextView)findViewById(R.id.minuteText);
secText=(TextView)findViewById(R.id.secondsText);
continueButton=(Button)findViewById(R.id.continueButton);
silenceButton=(Button)findViewById(R.id.silenceButton);
snackbar=Snackbar.make(view,"Unutma! eğer uygulamadan çıkarsan süren sıfırlanır! ",Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("Tamam", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
snackbar.show();
countDownTimer=new CountDownTimer(mins,1000) {
@Override
public void onTick(long millisUntilFinished) {
minText.setText((formatTimeMinutes(millisUntilFinished)));
secText.setText(formatTimeSeconds(millisUntilFinished));
futureMins=millisUntilFinished;
}
@Override
public void onFinish() {
onBreak=true;
if (onBreak){
Log.i("INFO","User in break time now!");
}
service.startService(new Intent(TimeActivity.this,TimeActivity.class));
quietButton.setVisibility(View.VISIBLE);
stopButton.setVisibility(View.INVISIBLE);
countDownTimer2=new CountDownTimer(300000,1000) {
@Override
public void onTick(long millisUntilFinished) {
minText.setText(formatTimeMinutes(millisUntilFinished));
secText.setText(formatTimeSeconds(millisUntilFinished));
}
@Override
public void onFinish() {
service.stopService(new Intent(TimeActivity.this,TimeActivity.class));
quietButton.setVisibility(View.INVISIBLE);
}
};
}
}.start();
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
futureMins=mins;
if (cameFromQuietButtonClick){
countDownTimer2.cancel();
cameFromQuietButtonClick=false;
}
if (cameFromQuietButtonClick==false) {
countDownTimer.cancel();
continueButton.setVisibility(View.VISIBLE);
stopButton.setVisibility(View.INVISIBLE);
}
});
continueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
countDownTimer.start();
continueButton.setVisibility(View.INVISIBLE);
stopButton.setVisibility(View.VISIBLE);
}
});
silenceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cameFromSilenceButtonClick=true;
service.stopService(new Intent(TimeActivity.this,TimeActivity.class));
quietButton.setVisibility(View.INVISIBLE);
stopButton.setVisibility(View.VISIBLE);
}
});
}
public String formatTimeMinutes(long mlSeconds){
output="";
seconds=mlSeconds/1000;
long minutes=seconds/60;
seconds=seconds%60;
minutes=minutes%60;
String minutesD=String.valueOf(minutes);
if (minutes<10) {
minutesD = "0" + minutes;
}
return minutesD;
}
public String formatTimeSeconds(long mlSeconds){
output="";
seconds=mlSeconds/1000;
seconds=seconds%60;
String secondsD=String.valueOf(seconds);
if (seconds<10) {
secondsD = "0" + seconds;
}
return secondsD;
}
}
答案 0 :(得分:2)
那是因为你用原来的&#34; mins&#34;构建了你的计时器实例。值。 CountDownTimer类无法更新,因此为了更新它的时间,您需要使用更新的值再次构造CountDownTimer对象。