简体   繁体   中英

How to run a function in background even i exit from the application in android?

I want to create a alarm function which is running in background even I exit from the application but not based on time. The function which checks for the value from the database, if the condition satisfies the alarm will arise... How to do this?

Seems that you're looking for a Service . They have the ability of being run even if your foreground app is not, but the disadvantage that you'll have to be aware it can be killed if the Android OS needs memory, especially if you're using too much resources.

It's also necessary to handle correctly the events where your Service may get created/destroyed, so you simply run your Service where you need and stop it when you no longer need it.

Some useful links:

Start a service which can run indefinitely.

your service would be a content observer or polling the database periodically.

Sqlite Database updates triggers Service to update via Content Observer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM