2

As the title says I am having the following problem. My foreground service is being killed when the activity that started it is swyped away from recent tasks list. I am starting a service with

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(notificationID, notification);
    return START_STICKY;
}

Can someone please tell me why is this happening and how can I make it so the service stays running when user swypes the activity away.

I don't have access to public void onTaskRemoved (Intent rootIntent) for some reason but I don't know what to do in that method anyway...

I am starting the service like this this and it's not a bound service

serviceIntent = new Intent(this, RecordingService.class);
startService(serviceIntent);

If little use case description helps I am trying to control sound recorder from a remote view in the notification bar so restarting a service is not an option since it should continue to record even if activity is destroyed.

BTW.I did tried starting a service in another process by android:process=":recordingProcess" and the service does continue to run then but I am suspecting this is not how you should do it.

Igor Čordaš
  • 5,785
  • 4
  • 42
  • 54
  • Related: [In android 4.4, swiping app out of recent tasks permanently kills application with its service . Any idea why?](http://stackoverflow.com/questions/20677781/in-android-4-4-swiping-app-out-of-recent-tasks-permanently-kills-application-wi) – blahdiblah May 20 '14 at 22:53

2 Answers2

1

Even i had the same issue and i had access to onTaskRemoved() function.Please check this link, "Process life cycle" topic. Try to return from onStartCommand() START_REDELIVER_INTENT, service will get start again.

paibhavesh
  • 167
  • 5
  • Thank you for suggestion, this did seem promising since I too believe there could be some flag that should be used but it's not working when I return that either. BTW. Do you know why I can't access onTaskRemoved , it doesn't list minimum api level in the docs so should be available. – Igor Čordaš Aug 02 '13 at 17:13
  • No it should work, but it will take some small amount of time to restart the service(may be of 1 o 2 min max).Actually it worked for me! and yes from API 14 you can access onTaskRemoved . – paibhavesh Aug 02 '13 at 17:18
  • Nope it doesn't work on Galaxy S2 with Android 4.1.2. Also I really need it to not stop because I am recording audio in that foreground service and controlling the recording process from a remote view (notification) – Igor Čordaš Aug 02 '13 at 17:20
  • Are you using startForeground() for notification ? – paibhavesh Aug 02 '13 at 17:22
  • Yes, it's in the example. If it's something about a notification I create it like this notification = new Notification(R.drawable.ic_launcher, "", System.currentTimeMillis()); contentView = new RemoteViews(getPackageName(), R.layout.notification_controller); notification.contentView = contentView; – Igor Čordaš Aug 02 '13 at 17:23
  • 1
    From android refeernce : A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.) – paibhavesh Aug 02 '13 at 17:29
  • Note this means that most of the time your service is running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service. An important consequence of this is that if you implement onStartCommand() to schedule work to be done asynchronously or in another thread, then you may want to use START_FLAG_REDELIVERY to have the system re-deliver an Intent for you so that it does not get lost if your service is killed while processing it. – paibhavesh Aug 02 '13 at 17:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34711/discussion-between-paibhavesh-and-psixo) – paibhavesh Aug 02 '13 at 17:31
0

From Android Developer Reference

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated.

How are you starting your service?

Community
  • 1
  • 1
Rajeev
  • 1,374
  • 1
  • 11
  • 18
  • A yes that too could be usefull to know. I am not using a bound service but just start it with serviceIntent = new Intent(this, RecordingService.class); startService(serviceIntent); – Igor Čordaš Aug 02 '13 at 16:59