nki
nki

Reputation: 192

Instantiate a function in an activity automatically for every 24 hours in Android

I am trying to instantiate a function(method1()) from an activity(act1.java) automatically for every 24 hours. I have checked some other tutorials, which demonstrated the usage of TimerTask and AlarmManager. But, there I am unable to understand the flow of the code. Can any one please provide me some sample code, or tutorials, so that I can get an idea of using TimerTask and AlarmManager.

These are the tutorials I have checked -

one

two

Thanks in advance.

Upvotes: 0

Views: 167

Answers (1)

M Sach
M Sach

Reputation: 34424

why not use ScheduledExecutorService which is designed to schedule commands to run after a given delay, or to execute periodically.

private final ScheduledExecutorService taskScheduler = Executors.newScheduledThreadPool(1);
 taskScheduler .scheduleAtFixedRate(yourRunnable, 0, 24, HOURS);

Also consider Quartz Scheduling which is useful if you are using scheduling jobs extensively in your project. More maintainable.

Upvotes: 1

Related Questions