gpt4 book ai didi

java - 我无法暂停并继续 CountDownTimer

转载 作者:行者123 更新时间:2023-11-29 19:41:46 25 4
gpt4 key购买 nike

您好,我正在尝试暂停和恢复我的 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;


}



}

最佳答案

那是因为您使用原始“mins”值构建了计时器实例。 CountDownTimer 类无法更新,所以为了更新它的时间,这次需要用更新后的值重新构造 CountDownTimer 对象。

关于java - 我无法暂停并继续 CountDownTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com