Android 根本无法运行倒计时
我有一个倒计时问题。我尝试了一些解决方案和文章在这个网站上,但他们从来没有为我工作。所以,请阅读我的密码 我也用过Android 根本无法运行倒计时,android,countdowntimer,timedelay,postdelayed,Android,Countdowntimer,Timedelay,Postdelayed,我有一个倒计时问题。我尝试了一些解决方案和文章在这个网站上,但他们从来没有为我工作。所以,请阅读我的密码 我也用过 handler.postDelayed(new Runnable() { 以前,这不是我的解决方案,但它只是正常工作 主要问题是: 我想做如下事情: (button pressed) do some codes1 delay1 do other codes2 delay2 go back to *do some codes1* again. 简而言之,
handler.postDelayed(new Runnable() {
以前,这不是我的解决方案,但它只是正常工作
主要问题是:
我想做如下事情:
(button pressed)
do some codes1
delay1
do other codes2
delay2
go back to *do some codes1* again.
简而言之,这是我真正的代码:
itimesec--;
setdelay();
irepeat--;
setrelax();
这是我的职责:
public void setrelax(){
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
itotalsnozee--;
TextToSpeechFunction(" "+itotalsnozee);
}
public void onFinish() {
itotalsnozee=fitotalsnozee;
isrelax=false;
TextToSpeechFunction("do again");
}
}.start();
yourCountDownTimer1.cancel();
}
我试着用一个变量代替50000,但无论如何都没有用
我试图将setrelax功能代码直接放入oncreate中,但从未奏效。
它跳到了
}.start();
yourCountDownTimer1.cancel();
每一次,我都会出去
我尝试了所有没有延迟功能的代码,它们运行正常
请问我的错误是什么…我查看了您的代码,没有发现任何重大错误,但请尝试此操作 而不是
.start();
使用
并删除CountTimer1。取消();
像这样:
public void setrelax(){
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
itotalsnozee--;
TextToSpeechFunction(" "+itotalsnozee);
}
public void onFinish() {
itotalsnozee=fitotalsnozee;
isrelax=false;
TextToSpeechFunction("do again");
}
};yourCountDownTimer1.start();
}
希望有帮助。下面是我们代码中运行otp定时器的代码。您可以复制粘贴我提到的注释,请遵循相同的操作
CountDownTimer countDownTimer; //define countDownTimer
countDownTimer.start(); // start countdown timer on some event like on click
String x="Your code will expire In";
String y="mins";
counttimer(); call the method counttimer which includes all the code of timer
public void counttimer(){
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
String text = String.format(Locale.getDefault(), x+" %02d : %02d "+y,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
phonever_timer.setText(text); //set timer into textview object
}
public void onFinish() {
phonever_timer.setText("Otp Expired..!");
}
};
您需要记住,在使用
CountDownTimer
时,代码不会按顺序执行,因为它是异步工作的(通过处理程序)
让我们分析一下你的代码。请在此处输入以下代码:
public void setrelax(){
// 1. Creating CountDownTimer
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
// 2. onTick called
...
}
public void onFinish() {
// 3. onFinish called
...
}
}.start();
// 4. CountDownTimer is cancelled.
yourCountDownTimer1.cancel();
}
将按以下顺序运行:
- 创建倒计时
- 倒计时被取消
- onTick反复呼叫49次(50000/1000=50-1)
- 恩菲什打电话来
延迟1
-->延迟完成后,执行其他代码2。那就做第二件事
您需要调用
CountDownTimer.onFinish()中的下一个代码,它非常实用。感激的
public void setrelax(){
// 1. Creating CountDownTimer
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
// 2. onTick called
...
}
public void onFinish() {
// 3. onFinish called
...
}
}.start();
// 4. CountDownTimer is cancelled.
yourCountDownTimer1.cancel();
}