Application gets killed when started by alarm manager

时间:2017-08-05 10:37:02

标签: android service broadcastreceiver alarmmanager

So basically I have got an application that has to run a service periodically (the service should be started every 3 hours for a few seconds). I use an alarm manager in the onDestroy() method of my service, and have a broadcast receiver that starts the service again. If my MainActivity is running (so the app is displayed on the screen), everything works fine. But if I close the app (by sliding it away in task manager) and I wait until the time the alarm is set to, I get the message box that the application has been closed instead.

Don't get me wrong, I think the alarm isn't killed when I close the app (because the message box appears right at the time the alarm is set), I assume that the problem has something to do with the os, and that it won't allow apps to run services in background when the original app is closed.

I've searched for a solution and saw lot's of ideas for fixing this problem, like giving the service a high priority by startForeground() so that it doesn't get killed, or to even use a wake lock, but none of them worked for me. I've also read about "protected apps" whose services aren't killed at all, but I assume that is way to complicated to do it anyways.

Thank you for your help!

My android version is 6.0.1.

0 个答案:

没有答案