Gökdeniz
Gökdeniz

Reputation: 1

Timertask period don't work at the time i set

Android studio Java TimerTask don't work at the time i set. I want it to run every 15 minutes in a service class even when application is closed too.

I want it to run every 15 minutes in a service class even when application is closed too. but it works like every hour instead of every 15 minutes

handler = new Handler();
        timer = new Timer();

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(() -> {
                    if (user.isMining() & checkTime(user.getStopFrom()) < 0) {
                        myRef.update("Balance", FieldValue.increment(0.000001));
                    }else{
                        onDestroy();
                    }
                });
            }
        };

        timer.schedule(timerTask,2000,convert(teamModel.getMultiplier()));

Upvotes: -2

Views: 95

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93542

TimerTask won't work when the app is closed. The TimerTask is an object within your app, if your app isn't running, the timer is no longer in memory. If you need to be woken up to do processing every 15 minutes, your options are JobScheduler, WorkManager, or an Alarm. Which of the 3 depends on your usecase.

Upvotes: 1

Related Questions