问题
I want a service which can run in the background until I stop, even if the component that started it is destroyed and also allows binding to the activities. How it is possible ?
As per android bound services document - there are three ways of creating bound service
- Extending Binder class.
- Using Messenger.
- Using AIDL.
I have created a bound service using messenger (2nd method). Activity is bind to service in its onStart() method and unbind in its onStop() method. Two way messaging (between activity and service) works properly. But problem is that when activity unbinds service, service is destroyed. But I want a service which can run indefinitely.
It is possible as android Services Dev Guide - "Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding."
I also implement onStartCommand() method in service and return START_STICKY, but it is never called. Looking at the lifecycle callbacks of bounded service in dev guide, there is no onStartCommand() callback method. Then how it is possible to run service until we stop and also allow binding?
I am using eclipse platform in fedora 15 OS.
Any Help.....
回答1:
You just need to start it with startService() somewhere. This will prevent it from being stopped automatically when there are no more bindings.
From the Service documentation, emphasis mine:
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.
As others have pointed out, it could still be killed by Android if resources are needed. You can "prioritize" your Service and make it less likely to be killed if you make it a foreground service.
回答2:
I've not used services with the messenger service, but I have bound to a remote service with a remote (AIDL) interface. My findings may be of some help. As my main activity and service are currently implemented, I bind to the service like you do with code like
mServiceConnected = bindService(new Intent("com.mypackage.MyService.SERVICE"), this,
Context.BIND_AUTO_CREATE);
My activity implements ServiceConnection
When I call unbindService(this) as the activity ends, then like you have found, the service's onDestroy() method is called.
If however, prior to the bindService line, I also explicitly start the service with
startService(new Intent("com.mypackage.MyService.SERVICE"));
then the unBind does not cause the service's onDestroy() to execute. It's still necessary call unbindService in the activity's onDestroy/Stop, otherwise you will leak a service connection.
In my case, presumably the service remains available for other applications to bind to via its remote interface.
回答3:
Service.onStartCommand callback will be called only when you start your service using startService method. As @NickT and @JoelF already pointed you need to call startService() besides the bindService() call somewhere in your client code (e.g. in onCreate).
You might also want to have a look at this (a little bit old, but still useful) article: "Double life of a service" and try the example program author provided.
回答4:
In order to perform communication between service and activity. You can also use Binder as mentioned in Official Android Example
http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
As official android documents suggests http://developer.android.com/guide/components/services.html#StartingAService
Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding.
This project implement this mixture of service (BothService) and shows that how can a Service run indefinitely and also allow binding with multiple activities.
https://github.com/shanrais/BothService
回答5:
If you add the "Ongoing Notification" in the Android App Drawer, then your app and service won't be killed.
Check out http://developer.android.com/guide/topics/ui/notifiers/notifications.html
回答6:
You can use service binder and make a single instance of connection at app instance level, in onCreate of the App, this will keep service alive. The service must be foreground service
来源:https://stackoverflow.com/questions/7292533/how-it-is-possible-service-run-indefinitely-and-also-allow-binding-in-android