从倒数计时器返回值

时间:2014-03-20 17:26:00

标签: java android return countdowntimer

我需要从countdowntimer返回值,以便我可以更新textView。我不知道如何做到这一点,我添加了一条评论,其中我尝试添加一个返回但没有运气。

public class BrewTimer extends Activity{

        public String secsRemain = "not set";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            System.out.println("Timer Created, we're in the onCreate Method.");
        }

        public void startBrewTimer(long remaningMillis) {
            // Start the Brew Timer
            System.out.println("Timer object fired!");

            new CountDownTimer(remaningMillis, 1000) {

                public void onTick(long millisUntilFinished) {
                    secsRemain = "seconds remaining: " + millisUntilFinished / 1000;
                    System.out.println(secsRemain);

                    return secsRemain; // <-- This is what I want to do
                }

                public void onFinish() {
                    System.out.println("Timer Finished!!!");
                }
            }.start();
        }

        public void stopBrewTimer() {
            //Stop the Brew Timer
        }

        }

2 个答案:

答案 0 :(得分:2)

  

我需要从countdowntimer返回值,以便我可以更新a   TextView的。

没有必要这样做。它运行在ui线程上。您可以在onTick本身更新textview。

示例:

public class MainActivity extends Activity { 
TextView tv;
  public String secsRemain = "not set";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);
    startBrewTimer(200000);     
}
private void startBrewTimer(long remaningMillis){
    CountDownTimer counter = CountDownTimer(remaningMillis, 1000){
        public void onTick(long millisUntilDone){
        secsRemain = "seconds remaining: " + millisUntilFinished / 1000; 
          tv.setText(secsRemain);                    
        }

        public void onFinish() {
            tv.setText("DONE!");

        }
    }.start();
}
 }

答案 1 :(得分:1)

您可以通过广播接收器获取值......如下所示, 首先创建自己的IntentFilter as,

    Intent intentFilter=new IntentFilter();
    intentFilter.addAction("YOUR_INTENT_FILTER");

然后创建内部类BroadcastReceiver as,

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    /** Receives the broadcast that has been fired */
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction()=="YOUR_INTENT_FILTER"){
           //HERE YOU WILL GET VALUES FROM BROADCAST THROUGH INTENT EDIT YOUR TEXTVIEW///////////
           String receivedValue=intent.getStringExtra("KEY");
        }
    }
};

现在在onResume()中注册您的广播接收器,

registerReceiver(broadcastReceiver, intentFilter);

最后在BroadcastReceiver中取消注册onDestroy()

unregisterReceiver(broadcastReceiver);

现在最重要的部分......你需要从需要发送值的地方发射广播.....所以这样做,

Intent i=new Intent();
i.setAction("YOUR_INTENT_FILTER");
i.putExtra("KEY", "YOUR_VALUE");
sendBroadcast(i);
如果你觉得这个问题令人满意,请不要忘记接受我的回答....欢呼:)