一旦取消定时器任务并恢复它,如何访问定时器值?

时间:2014-03-16 13:49:50

标签: android android-intent android-activity

我有活动A和B.当我在活动A中暂停计时器时,它会停止计时器,我想在活动B中启动恢复按钮时再次恢复它。我已经看过教程,但我刚刚找到教程在同一活动中。

活动A有倒数计时器和暂停按钮:

    // implements CountdownTimer
    final CountDownTimer timer = new CountDownTimer(30000, 1000) {
        TextView timeLeft = (TextView) findViewById(R.id.time_left_value);

        public void onTick(long millisUntilFinished) {
            timeLeft.setText("" + millisUntilFinished / 1000);
        }

        public void onFinish() {
        //  TODO: set intent to next screen
        Intent i = new Intent(A.this, C.class);
        startActivity(i);
        }
    }.start();


    // Pause Button
    Button pause_btn = (Button) findViewById(R.id.pause_btn);
    pause_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //Move to the next view!
            Intent i = new Intent(A.this, B.class);
            startActivity(i);
            timer.cancel();
        }
    });  

活动B有简历按钮:

    Button resume_btn = (Button) findViewById(R.id.resume_btn);
    resume_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //Move to the next view!
                finish();
            }
        });  

2 个答案:

答案 0 :(得分:0)

如果没有被误解,你可以添加以下内容:

  1. 跟踪"时间"并在取消之前将其归还:

    private class MyCountDownTimer extends CountDownTimer{
            TextView timeLeft = (TextView) findViewById(R.id.time_left_value);
            private long  millisUntilFinished = -1;
            public void onTick(long millisUntilFinished) {
                timeLeft.setText("" + millisUntilFinished / 1000);
                this.millisUntilFinished = millisUntilFinished;
            }
    
            public long getMillisUntilFinished() {
              return millisUntilFinished;
            }
    
            public void onFinish() {
              millisUntilFinished = -1;
            //  TODO: set intent to next screen
            Intent i = new Intent(A.this, C.class);
            startActivity(i);
            }
        }
    
    
    
    final MyCountDownTimer timer = new MyCountDownTimer(30000, 1000) 
    
  2. 在开始ActivityB之前,取消时间并致电timer.getMillisUntilFinished(),将值放入Intent并将其传递给ActivityB

  3. 如果是value != -1,那么算一算,创建一个新的CountDownTimer并使用新值启动它
  4. 编辑:第2点示例:

    pause_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //Move to the next view!
                Intent i = new Intent(A.this, B.class);
                i.putExtra("time_left",timer.getMillisUntilFinished());
                startActivity(i);
                timer.cancel();
            }
    });
    

答案 1 :(得分:0)

活动A

pause_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //Move to the next view!
            Intent i = new Intent(A.this, B.class);
            i.putExtra("time_left",timeLeft);
            startActivity(i);
            timer.cancel();
        }
});

关于活动B

long timeLeft = getIntent().getExtras().getLong("time_left");

Button resume_btn = (Button) findViewById(R.id.resume_btn);
resume_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                final CountDownTimer timer = new CountDownTimer(timeLeft , 1000) {
                       //your code
                }.start();
    }


                finish();
           }
});