Returning Value From Countdown Timer
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. pub
Solution 1:
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:
publicclassMainActivityextendsActivity {
TextView tv;
publicStringsecsRemain="not set";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
startBrewTimer(200000);
}
privatevoidstartBrewTimer(long remaningMillis){
CountDownTimercounter= CountDownTimer(remaningMillis, 1000){
publicvoidonTick(long millisUntilDone){
secsRemain = "seconds remaining: " + millisUntilFinished / 1000;
tv.setText(secsRemain);
}
publicvoidonFinish() {
tv.setText("DONE!");
}
}.start();
}
}
Solution 2:
You can get values through broadcast receiver......as follows,
First create your own IntentFilter
as,
Intent intentFilter=newIntentFilter();
intentFilter.addAction("YOUR_INTENT_FILTER");
Then create inner class BroadcastReceiver
as,
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
/** Receives the broadcast that has been fired */
@Override
publicvoid 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=newIntent();
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 :)
Post a Comment for "Returning Value From Countdown Timer"