Android Service can be bind without start?

冷暖自知 提交于 2019-11-28 11:38:32

I found the solution, so am sharing with all of you.

There are 2 types of Services : One where you start and stop. This can be started and stopped only once in an application. Other you Bind and Unbind as required N number of times. My Service is of second type. But just bind and unbind doesn't do the job. The service first needs to be started then only it cna be bound and unbound. So on start of app or whereever appropriate, Start the service. Then Bind when required. When don with it, Unbind it. That Bind-Unbind circle can go on. And Finally when you are sure you don't need it or at the end of the app Stop the Service. So the flow comes as Start -> Bind -> Unbind -> Stop <-

Hope this helps someone.

Mark

Yes.

bindService(new Intent(this, MyService.class), mConnection, 0);

AFAIK, this will always return true (assuming there is no problem with MyService)

There are two scenarios:

  1. The service has previously been started - mConnection's onServiceConnected() is called
  2. The service has NOT previously been started - mConnection's onServiceConnected() is NOT called and the service is NOT started. However, as soon as the service is started (by some other means), the onServiceConnected() is then called

In practice, when I call this method, I assume the service is not started until the onServiceConnected() method is called.

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