Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在PreExecute上启动倒计时,在PostExecute上结束倒计时_Android_Timer - Fatal编程技术网

Android在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

我正在尝试在PreExecute上显示倒计时计时器,并在PostExecute上结束计时器。我有下面的代码,在PreExecute上运行良好,但我找不到任何方法在PostExecute上关闭此计时器。下面是我的计时器代码

            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();
}