pedja
pedja

Reputation: 3413

executing code every 24h, but only if app is opened

I want to execute specific code in my app every 24h.

I know how to use AlarmManager but problem is I don't want that code to be executed if app isn't running.

I want it to be executed when app starts but only if 24h has passed after last execution.

It doesn't matter if more 24h has passed

For example if user opens my app for the first time code will execute. If it opens it again after 20min nothing will happen. if it opens it after 24h or more code will be executed.

Upvotes: 0

Views: 116

Answers (2)

Alankar Srivastava
Alankar Srivastava

Reputation: 864

Why don't you use Shared Preferences storage option provided by Android platform to store last access information and then act accordingly. For more details refer this link: Android developer reference for Storage Options

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533660

I would save to a file, the time the program was started. You can check this before writing to see how long it has been.

Note: if there is a task you want to run at least 24h apart, you really want the last time the task was performed. e.g. if the application is opened every 8h you still want it to run.

Upvotes: 1

Related Questions