Sorry for my english. I have timer and i wand if i click timer is on if i click again timer off. But my timer on only one time. If i click again(off timer) i have exception like this:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Timer was canceled
at java.util.Timer.scheduleImpl(Timer.java:561)
at java.util.Timer.schedule(Timer.java:481)
at installation.ConnectDevice.callAsynchronousTask(ConnectDevice.java:211)
at installation.ConnectDevice$1.onClick(ConnectDevice.java:153)
at android.view.View.performClick(View.java:4240)
...
I dont know why its not work, please help. Below my class
My class
private Timer timer;
int time = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.i_connect_device);
timer = new Timer();
// my botton
includeDevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (time < 1) {
callAsynchronousTask();
time++;
}
if (time > 0) {
stopTimer();
time--;
}
}
});
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
Log.e("Timer is work", "Timer is work");
// GetMsgs performBackgroundTask = new GetMsgs();
// PerformBackgroundTask this class is the class
// that extends AsynchTask
// performBackgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000 * 10); // execute in every
// 50000 ms
}
public void stopTimer() {
timer.cancel();
}