How it is possible Service run indefinitely and also allow binding in android?

岁酱吖の 提交于 2019-11-27 12:02:44

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.

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.

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.

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!