Returning value from countdown timer
Asked Answered
M

2

1

I need to return the value from my countdowntimer so I can update a textView. I'm not sure how I can do this, I've added a comment where I've tried adding a return but no luck.

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
        }

        }
Munich answered 20/3, 2014 at 17:26 Comment(1)
Since the return type is void, public void onTick(long millisUntilFinished) { you can't return anything. But see Raghunandan's answer.Myrmecophagous
S
2

I need to return the value from my countdowntimer so I can update a textView.

There is no need to do that. It runs on the ui thread. You can update textview in onTick itself.

Example:

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();
}
 }
Stilly answered 20/3, 2014 at 17:28 Comment(2)
Thanks for that, I put the timer in its own class thinking that I could then create an object and also various methods for it. Is the way you've described the typical way countdown timers are used? I could put the countdown timer code within the Activity and use the code you've provided and it would work but thought having it as an object or singleton would be better for more versatility?Munich
@Munich the above works. You don't need singleton for this.Stilly
M
1

You can get values through broadcast receiver......as follows, First create your own IntentFilter as,

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

Then create inner class 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");
        }
    }
};

Now Register your Broadcast receiver in onResume() as,

registerReceiver(broadcastReceiver, intentFilter);

And finally Unregister BroadcastReceiver in onDestroy() as,

unregisterReceiver(broadcastReceiver);

Now the most important part...You need to fire the broadcast from wherever you need to send values..... so do as,

Intent i=new Intent();
i.setAction("YOUR_INTENT_FILTER");
i.putExtra("KEY", "YOUR_VALUE");
sendBroadcast(i);

don't forget to accept my answer if you find this ans satisfactory....cheers :)

Mashie answered 20/3, 2014 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.