xvicient
xvicient

Reputation: 81

Android notifications on background service

I'm developing an app that shows notifications recursively. The user can define how often he want to receive notifications and the background service controls with a recursive handler this time to know when to send notifications.

The problem is when the android's memory manager destroys my service when it deems necessary and the application stops sending notifications.

How can I send notifications from a background service and avoid android's memory manager destroys it? Any idea?

Upvotes: 0

Views: 743

Answers (2)

Andrii Khrystovyi
Andrii Khrystovyi

Reputation: 71

If you want have implemented you feature please take a look to AlarmManager class.

You can use it where you want to have your application code run at a specific time, even if your application is not currently running.

So, please do not keep a Service all the time, just show a notification and post an Intent with information about your Service to AlarmManager (please do not forget call stopService() after all). Then AlarmManager will start you Service when it needed and the Service will do the case again (notification and post Intent to AlarmManager).

P.S: JobScheduler class

Upvotes: 1

tynn
tynn

Reputation: 39843

You can use startForeground() to mark the service important to the system.

You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.

Upvotes: 0

Related Questions