0

Is it possible to create a service in Android, which can not be started programmatically via startService call? I mean, that an application with this service and the service itself should be started explicitly by a user only, and should be accessible only via bindService, in the case if the service is already started at the moment.

Stan
  • 8,683
  • 9
  • 58
  • 102

2 Answers2

1

Can't be started by startService? How else would it be started?

I suspect what you want is to ensure that only your own app can start it and not any external non-written-by-you apps. If that's the case, set

android:exported="false"

in your AndroidManifest.xml file for each Service you want protected in this way.

Argyle
  • 3,324
  • 25
  • 44
  • No, I want to make sure that any app (even my another app) can not start the application which hosts the service. – Stan Sep 10 '12 at 19:29
  • as for now it makes no sense for me. if no app can start your app how launcher (being app) could do that? Anyway I believe you got quite odd requirements and I also believe it can be achieved in less tricky way – Marcin Orlowski Sep 10 '12 at 19:32
  • @WebnetMobile.com, well, strictly speaking, only the host application should have start the service, and if it's started, it's available to other applications via bindService. If not possible, I'll look into another possibilities. – Stan Sep 10 '12 at 19:41
1

Is it possible to create a service in Android, which can not be started programmatically via startService call?

Not really. You could presumably call stopSelf() from onStartCommand(), though I have never tried this and it may cause unexpected problems.

the service itself should be started explicitly by a user only

There is no way for a user in Android to start a service directly, only by means of some app, such as your own app.

and should be accessible only via bindService

You have no means of preventing a call to startService(). If your service is not exported, no other application will be able to start or bind to it.

and if it's started, it's available to other applications via bindService

Only if you export it. As @Argyle notes, if you do not want other applications starting or binding to the service, do not export it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I need the service for my other apps, so it must be exported. But if the host application (service provider) is not launched by user, the service should not be available, and could not be instantiated via startService. As I can understand, this is impossible. Right? – Stan Sep 10 '12 at 19:51
  • @Stan: "But if the host application (service provider) is not launched by user, the service should not be available, and could not be instantiated via startService" -- if it is exported, any app can start the service via `startService()`. You may wish to consider protecting the service with a signature-level permission, if you only want some suite of apps to be able to interact with it. – CommonsWare Sep 10 '12 at 19:58
  • I think I found more precise answer to my question, and it is yours - http://stackoverflow.com/questions/8272173/androidenable-and-services. – Stan Sep 13 '12 at 13:11