3

I've developed an android application with a service that runs in the background the service is called like that

startService(new Intent(getApplicationContext(),myService.class));

but when I remove the app from the recent apps, the service stops and it crashes if I test it.. is there a way to prevent the service from being destroyed when swiping the app from the recent apps?

thank you

geekslot
  • 213
  • 1
  • 4
  • 11
  • 1
    removing the app from the recents list is basically a task killer, you should handle the crash in any case but when the app is removed a call goes to `onTaskRemoved` and you should stop what you are doing to prevent crashes. http://developer.android.com/reference/android/app/Service.html#onTaskRemoved(android.content.Intent) – tyczj Jan 08 '15 at 17:21
  • 1
    I bet the crash is due to the null Intent passed to onStartCommand... – pskink Jan 08 '15 at 17:46
  • @tyczj is there anyway to prevent that from happening? like running the app in the notifications bar, something like the soundcloud app, when you remove it, the song doesn't stop. thx for your reply – geekslot Jan 08 '15 at 17:58
  • The Service, possibly, don't get deleted/destroyed. The intent passed through onStartCommand should be null, and thus causing you to crash. The way for you to get your service killed is to do "Force Stop". Swiping away only kills the task, not the process. Process might be killed by the system to reclaim resources. – stdout Oct 25 '15 at 12:59

1 Answers1

5

You can register a service to run on a repeating cycle using AlarmManager

http://developer.android.com/reference/android/app/AlarmManager.html

The service will run even if the application is not running.

mjstam
  • 1,049
  • 1
  • 6
  • 5