PROBLEM I have problems stopping the timer during development in Android.
The timer is already null when it comes to stopping it.
Then I move the timer initialization outside the method, just like TimerTask, which solves the zero problem, but still does not cancel when timer.cancel();
is called on it timer.cancel();
.
The following is an example where the timer is already null when it comes to stopping recording.
Timertask
My TimerTask
initialized inside the class, but outside the method and codes below ...
private TimerTask task = new TimerTask() { @Override public void run() { Log.e("TRACK_RECORDING_SERVICE","Timer Running"); } };
Timer and timer start
Then I have a startRecroding method that is called when I want to start the timer ...
public void startRecording(){ timer = new Timer("Message Timer"); timer.scheduleAtFixedRate(this.task, 0, 1000); }
Stop timer
Then I call the method below when I want to stop the timer ...
public void stopRecording() { if (timer != null) { timer.cancel(); timer = null; } else { Log.e("TRACK_RECORDING_SERVICE","Timer already null."); } }
Any help would be greatly appreciated.
source share