Android将Countdown Timer的值传递给2个或更多活动

时间:2013-02-05 20:38:22

标签: android countdowntimer

我正在尝试将countdownTimer值从活动1传递到活动2,并从活动1中获取的值启动活动2中的倒计时Timer,但我不知道如何启动将活动2中的计时器指向变量,这是我的活动1的代码

    new CountDownTimer(20000, 1000) {
        public void onTick(long millisUntilFinished) {
            tv.setText("seconds remaining: " + millisUntilFinished / 1000);

        }
        public void onFinish() {
            tv.setText("done!");
        }
     }.start();

    next.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this, Page2.class);
            i.putExtra("timervalue", tv.getText());
            startActivity(i);
        }

    });

,这是活动2中的代码

    Bundle bundle = getIntent().getExtras();  
    long value = Long.parseLong(bundle.getString("timervalue"));
    new CountDownTimer(value, 1000) {
        public void onTick(long millisUntilFinished) {
            tv.setText("seconds remaining: " + millisUntilFinished / 1000);
        }
        public void onFinish() {
            tv.setText("done!");
        }
     }.start();  

我还尝试使用Intet而不是Bundle

来制作活动2
    Intent results = this.getIntent();
    String s = results.getExtras().getString("timervalue");
    long value = Long.parseLong(s);

    new CountDownTimer(value, 1000) {
        public void onTick(long millisUntilFinished) {
            tv.setText("seconds remaining: " + millisUntilFinished / 1000);
        }
        public void onFinish() {
            tv.setText("done!");
        }
     }.start();  

我认为我在putExtra和getExtra时遇到的错误但是我不确定,我是java的初学者所以请任何帮助...... 非常感谢

2 个答案:

答案 0 :(得分:1)

这是正确的代码

在Activity1中

long remainingtime;

在onCreate下

 new CountDownTimer(20000, 1000) {

        public void onTick(long millisUntilFinished) {
            Counter.setText("Time left: " + millisUntilFinished / 1000);
                remainingtime=millisUntilFinished;
        }

        public void onFinish() {
        Counter.setText("done!");
    }}.start();  



public void onClick(View v) {

Intent i = new Intent(Activity1.this, Activity2.class);

   Bundle timetransfer=new Bundle();
    timetransfer.putLong("timer", remainingtime);
        i.putExtras(timetransfer);

活性2

Under onCreate



Bundle timetransfer = getIntent().getExtras();

                long time =timetransfer.getLong("timer");


        new CountDownTimer(time, 1000) {

            public void onTick(long time) {
                Counter2.setText("Time Left: " time / 1000);
            }

            public void onFinish() {
                Counter2.setText("Time is UP!");

                finish();    }

         }.start();

你必须删除这部分:

long_remaingtime=millisUntilFinished / 1000;

并且只是

long_remaingtime=millisUntilFinished;

因为在你的情况下,你已经将大约20000(millisUntilFinished)与1000分开,并且根据时间的推移你得到大约20秒, 然后在下面你已经把代码再次将这个数字除以1000。因此,在下一个活动中,您会自动获得时间已过的错误。

答案 1 :(得分:0)

目前,您正在将"seconds remaining: "+...传递给下一个活动并尝试将其解析为Long意味着您将在下一个活动中获得NumberFormatException。使用额外的变量将CountDownTimer重命名时间传递给下一个Activity:

public long long_remaingtime=20L;
 new CountDownTimer(20000, 1000) {
        public void onTick(long millisUntilFinished) {
            tv.setText("seconds remaining: " + millisUntilFinished / 1000);
            long_remaingtime=millisUntilFinished / 1000; //<<< 
        }
        public void onFinish() {
            tv.setText("done!");
            long_remaingtime=20L; //<< set default here
        }
     }.start();

    next.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this, Page2.class);
            i.putExtra("timervalue", long_remaingtime);
            startActivity(i);
        }

    });

并在下一个Activity中获取timervalue:

Intent results = this.getIntent();
long value = results.getExtras().getLong("timervalue");