Android在PreExecute上启动倒计时,在PostExecute上结束倒计时
我正在尝试在PreExecute上显示倒计时计时器,并在PostExecute上结束计时器。我有下面的代码,在PreExecute上运行良好,但我找不到任何方法在PostExecute上关闭此计时器。下面是我的计时器代码Android在PreExecute上启动倒计时,在PostExecute上结束倒计时,android,timer,Android,Timer,我正在尝试在PreExecute上显示倒计时计时器,并在PostExecute上结束计时器。我有下面的代码,在PreExecute上运行良好,但我找不到任何方法在PostExecute上关闭此计时器。下面是我的计时器代码 new CountDownTimer(240000, 1000) { // adjust the milli seconds here public void onTick(long millisUntilFinishe
new CountDownTimer(240000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
String time=""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
Toast.makeText(context, time, Toast.LENGTH_SHORT).show();
}
public void onFinish() {
Intent i=new Intent(context,MainActivity.class);
context.startActivity(i);
Toast.makeText(context, "oops! Pls check your net connection", Toast.LENGTH_SHORT).show();
}
}.start();
创建计时器的全局变量
CountDownTimer mTimer = null;
关于PreExecute()write
现在开始postExecute()写入
@Override
protected void onPreExecute()
{
mTimer = new CountDownTimer(240000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
String time=""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
Toast.makeText(context, time, Toast.LENGTH_SHORT).show();
}
public void onFinish() {
Intent i=new Intent(context,MainActivity.class);
context.startActivity(i);
Toast.makeText(context, "oops! Pls check your net connection", Toast.LENGTH_SHORT).show();
}
}.start();
}
@Override
protected void onPostExecute()
{
mTimer.cancel();
}