Question

I currently has a repeat schedule task that runs every 30 second. There is a button to stop, resume and start of the task. The problem is either use the timer.cancel() or timer.purge() still not handling the start/pause function

gpsTimer.scheduleAtFixedRate(new TimerTask() {
                            @Override
                            public void run() {
                                distanceCount = (member.stepLength / 100000) * stepCount; // cm to km
                                caloriesCount = 0.47 * member.weight * 2.20462262 * distanceCount;

                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Log.d("test1","t");
                                        distance.setText(String.format("%.2f", distanceCount));
                                        calories.setText(String.format("%.2f", caloriesCount));
                                    }
                                });
                            }
                        }, 0, 10000);


gpsTimer.purge();

When I press start it run the scheduleAtFixed Rate, when I press stop/pause, it run the purge, however, the task is not stopped, I try gpsTimer.cancel(), but it throw exception when I run it again. Thanks for helping

Was it helpful?

Solution

May this help you:

Once a TimerTask is cancelled, same cannot be started again..
You have to create a new instance if you want to reuse the same object again...

Or you can use ScheduledExecutorService (from Java 5+).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top