1

I have written a service in Android which seems to be working fine and till the time its 'Process' is alive, as soon as its process is killed service doesn't do any operation even if it is alive. Through phone "Apps" menu I can see that service stays alive, it shows "Process 0 Service 1".

I tried almost everything like service, intent-service, binding the service, sticky service, tried to run service on another thread but no use.

Can anybody guide me into this?

Thanks in advance, Prasad.

  • Have a look in details on Services http://developer.android.com/reference/android/app/Service.html – zIronManBox Jul 27 '15 at 03:47
  • I think, you are trying to create a global service, instead you might have created a local services, which is bound to your process, hence its being killed. – zIronManBox Jul 27 '15 at 03:48
  • Then you would have to run it in separate process, follow this: http://developer.android.com/guide/topics/manifest/service-element.html – zIronManBox Jul 27 '15 at 03:49

3 Answers3

0

By default Service runs on the same process as your app. If you stop the process then your Service is also stopped. If you want to keep your Service running even when the process is killed, you can run it in a separate process.

Refer: http://developer.android.com/guide/topics/manifest/service-element.html

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Thank you for your quick response Gennadii, yes according to the link shared by you, in manifest I had set “android:process=”:My_Process”. To test whether my service is operational or not I create notification every 5 seconds. I see that till the process is alive notifications are shown successfully but as soon as I kill the process by sliding it from recent view, notification stops. I go to Apps and see that it shows Service is alive and my question is if that Service is alive then why it doesn’t show me any notification as it was showing earlier when its process was alive. – prasadonstakoverflow Jul 27 '15 at 04:47
0

AFAIK, if you kill your application from Force Stop button in App Infor (App manager), there's no way for your Service to recreate.

START_STICKY is used for services that are explicitly started and stopped as needed

That means when the system kills your app, it will recover it if there's enough memory. However, when users kill the app process, I dont think the system will revive it.
Maybe there's a hack way to start your service after being killed by users: Using AlarmManager with PendingIntent.getService to schedule a checker which will check your service status every minute. I dont try this solution, but you should be careful with it. Hope it help.

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
  • Thank you for your quick response, yes I tried that PendingIntent technique as well but that did not help. When I kill the process by sliding it through recently-viewed list, I see service doesn’t get killed, App manager shows it is alive “Process 0 Service 1” but then I wonder if that service is not killed then why doesn’t it perform the operation it is supposed to perform in my case I create notifications. – prasadonstakoverflow Jul 27 '15 at 04:48
  • 1
    Hi, may I know why did you mention "be careful with it"? Actully it worked for me, I found it at this link - http://stackoverflow.com/questions/20592366/the-process-of-the-service-is-killed-after-the-application-is-removed-from-the-a – prasadonstakoverflow Jul 27 '15 at 08:32
0

After many attempts trying with different ways when I was not able to get the desired result I kept searching for the solution and here on StackOverflow I got a work around for this at the link given below -

The process of the service is killed after the application is removed from the application tray

This worked for me. After task is removed now service gets restarted and keeps working in background until it is stopped forcefully !!

Community
  • 1
  • 1