Android Service can be bind without start?

前端 未结 2 981
孤街浪徒
孤街浪徒 2020-12-10 18:24

In many articles, tutorials, docs, have read so far, that we call startService() or bindService(), both starts the service. We can call both also, but that\'s a different st

相关标签:
2条回答
  • 2020-12-10 18:40

    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.

    0 讨论(0)
  • 2020-12-10 18:51

    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.

    0 讨论(0)
提交回复
热议问题