我有一个倒数计时器30秒,进度条工作正常,但当应用程序进入暂停状态计时器仍然有效,但我想当应用程序进入暂停状态计时器和进度条将停止,当恢复应用程序它将从它停止的地方。我怎么能这样做。
这是我的代码: -
@SuppressLint("SetTextI18n")
private void init() {// initialize view controls
m_timerProgress = (ProgressBar) findViewById(R.id.pb);// finding ID of progressbar
final TextView tv = (TextView) findViewById(R.id.tv);// finding Id of textview to show timer text
tv.setText("00:00");// set timer text
//Initialize a new CountDownTimer instance
long m_MillisInFuture = 30000;// timer value
long m_CountDownInterval = 1000;// timer break up
m_oTimer = new CountDownTimer(m_MillisInFuture, m_CountDownInterval) {
public void onTick(long millisUntilFinished) {
@SuppressWarnings("UnusedAssignment") long millis = millisUntilFinished;
@SuppressLint("DefaultLocale") String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
System.out.println(hms);
tv.setText(hms);
//Another one second passed
//Each second ProgressBar progress counter added one
m_n_ProgressStatus += 1;
m_timerProgress.setProgress(m_n_ProgressStatus);
}
public void onFinish() {// when timer finished
Intent i = new Intent(COtpAutoVerificationScreen.this, COtpManualVerificationScreen.class);
startActivity(i);
}
}.start();// start timer
// retreive progress bar count........
int progressBarMaximumValue = (int) (m_MillisInFuture / m_CountDownInterval);
//Set ProgressBar maximum value
//ProgressBar range (0 to maximum value)
m_timerProgress.setMax(progressBarMaximumValue);
//Display the CountDownTimer initial value
tv.setText(progressBarMaximumValue + "Seconds...");
}
答案 0 :(得分:0)
在onPause中取消定时器。使用全局变量来跟踪时间。当计时器运行时,按间隔减少全局变量。
在onResume中使用全局变量作为计时器值启动倒计时器。您的进度条也可以保存最后位置并从那里开始操作。
private long currentMillis=30000;
更改您的init方法以获取一个长参数并将视图初始化移到
之外private void init( long millis) {
//Initialize a new CountDownTimer instance
long m_MillisInFuture = millis;// timer value
long m_CountDownInterval = 1000;// timer break up
m_oTimer = new CountDownTimer(m_MillisInFuture, m_CountDownInterval) {
public void onTick(long millisUntilFinished) {
@SuppressWarnings("UnusedAssignment") long millis = millisUntilFinished;
long hmsLong=,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished));
@SuppressLint("DefaultLocale") String hms = String.format("%02d:%02d",TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),hmsLong );
System.out.println(hms);
currentMillis=hmsLong;
tv.setText(hms);
//Another one second passed
//Each second ProgressBar progress counter added one
m_n_ProgressStatus += 1;
m_timerProgress.setProgress(m_n_ProgressStatus);
}
.....
m_oTimer.start();
然后你的OnPause应该是
@Override
public void onPause() {
super.onPause();
m_oTimer.cancel();
}
和onResume将是
@Override
public void onResume() {
super.onResume();
init(currentMillis)
}
这应该做你想要的。
答案 1 :(得分:0)
You can achieve this by following steps.
make m_MillisInFuture member variable
public void OnPause(){
m_oTimer.stop();
m_oTimer = null;
m_MillisInFuture- = m_n_ProgressStatus*1000;
}
public void onResume(){
//here create your timer with updated m_MillisInFuture value and start
startTimer();
}
private void startTimer(){
m_oTimer = new CountDownTimer(m_MillisInFuture, m_CountDownInterval) {
public void onTick(long millisUntilFinished) {
@SuppressWarnings("UnusedAssignment") long millis = millisUntilFinished;
@SuppressLint("DefaultLocale") String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
System.out.println(hms);
tv.setText(hms);
//Another one second passed
//Each second ProgressBar progress counter added one
m_n_ProgressStatus += 1;
m_timerProgress.setProgress(m_n_ProgressStatus);
}
public void onFinish() {// when timer finished
Intent i = new Intent(COtpAutoVerificationScreen.this, COtpManualVerificationScreen.class);
startActivity(i);
}
}.start();
}