following is my code:
In MainActivity.class
private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {
if(CheckedButtonId==R.id.radiobutton_on){
Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
alert_on = true;
display_alert();
}
else{
alert_on = false;
}
}
};
public void display_alert(){
int delay = 10000;
Timer timer =new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
startService(myIntent);
}
}, delay,10000);
}
MyService.class
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(MainActivity.alert_on)
Toast.makeText(getApplicationContext(), "Alert is On", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Alert is Off",Toast.LENGTH_LONG).show();
}
I am checking the onCheckedChangeListener
and calling display_alert
function according to the checked radio button.
In display_alert
function I was using timer scheduling at fixed rate of 10 seconds and calling myservice
class.
So once I checked the radio_on
button, it calls my display_aler
t function and got the "alert on" in toast. So its working fine.
If I again check radio_off
button and then check the radio_on
button, I am getting the "alert on" toast, but the toast is coming twice. Similarly if I again select the radio_on
button, the toast is displaying for the third time, but I need only once. This was due to that the timer is running for every 10 seconds. I want to stop the timer once I click the radio_off
button.
The problem is, once I started the timer, I was not stopping it further. When and how shall I cancel the timer tasks in my class?
First, create your TimerTask on a separate way:
class MyTimerTask extends TimerTask {
public void run() {
Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
startService(myIntent);
}
}
Declare it somewhere, of course visible to your OnCheckedChangeListener:
MyTimerTask myTimerTask;
Schedule it:
public void display_alert() {
int delay = 10000;
myTimerTask = new MyTimerTask();
Timer timer = new Timer();
timer.scheduleAtFixedRate(myTimerTask, delay, 10000);
}
And then modify your code to:
private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {
if(CheckedButtonId==R.id.radiobutton_on){
Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
alert_on = true;
display_alert();
}
else{
myTimerTask.cancel();
alert_on = false;
}
}
};
if(timerTask!=null){
timer.cancel();
}
you can use the code to stop/cancel timer
First create a class extends TimerTask, make object of it and handle the operation on that object.
int delay = 10000;
Timer timer =new Timer();
myTimer mTask= new myTimer();
timer.scheduleAtFixedRate(mTask, delay, 10000);
public class myTimer extends TimerTask
{
@Override
public void run() {
Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
startService(myIntent);
}
}
when your mTask works get over just
if(mTask!=null){
mTask.cancel();
}
thats it.
I am able to stop a timer and a task using following code:
if(null != timer)
{
timer.cancel();
Log.i(LOG_TAG,"Number of cancelled tasks purged: " + timer.purge());
timer = null;
}
if(task != null)
{
Log.i(LOG_TAG,"Tracking cancellation status: " + task.cancel());
task = null;
}
来源:https://stackoverflow.com/questions/10311087/how-to-stop-timer-android