3

I am looking to create a system service while using system permissions. The service should be bounded to an activity and fits the following requirements

  1. Service will not be killed by the system.
  2. When requested, service can be stopped.

In order to achieve the first requirement I have created my service by using Context.bindService() creating a binded system service and added android:persistent="true" to the service manifest informing the system that this service should remain running at all time.

Unfortunately after adding android:persistent="true" I'm unable to stop the service neither by Context.unbindService() or by calling Context.stopService()

Is it possible to stop a service with android:persistent="true" and if not what should be my approach in order to achieve both of the requirements

Shulmant
  • 31
  • 2
  • please noticed I'm signing the service with the system key allowing the use of persistence while creating a system service – Shulmant Dec 28 '14 at 16:01

1 Answers1

0

I've just been through something similar. As per this answer, you need to call stopService() and then unBindService() -- call both in that order. Put these in the host activity's onDestroy method. Also, just in case your service is awakened periodically by an AlarmManager, as was mine, make sure to call cancel() on that as well.

As you probably know, you can check for running services in the ApplicationManager. Force stop any services you may have running with older code when testing these changes.

Regarding the persistent flag, I think, as per this, that it is only in effect for system app developers. In a regular marketplace app, I think the flag is ignored.

Community
  • 1
  • 1
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • Jason I've created my service by calling Context.bindService() therefore calling Context.unbindService() should of destroyed the process as mentioned in [Android documentation](http://developer.android.com/guide/components/services.html) "If a component calls bindService() to create the service (and onStartCommand() is not called), then the service runs only as long as the component is bound to it. Once the service is unbound from all clients, the system destroys it." Unfortunately adding the android:persistent="true" Prevents me from stopping the service in such a method. – Shulmant Dec 28 '14 at 16:15