5

I have done a lot of research on this, but still not sure if I can connect to an android service synchronously. startService() and bindService() service calls are asynchronous by nature and what I'm trying to do is make this behavior synchronous.

I tried writing startService(), bindService() and ServiceConnection object initialization (contains onServiceConnected() etc.) in a separate thread which is started from the main thread. And the main thread waits until onServiceConnected() is invoked on another thread and service object gets initialized, but since onServiceConnected() is only invoked by the main thread (http://developer.android.com/reference/android/content/ServiceConnection.html) which is blocked due to wait call, it forms a deadlock.

Does anyone know if my above reasoning is correct/incorrect? Is there another way to achieve the synchronous behavior? Or the only option is to invoke service functions after onServiceConnected() callback happens?

EDIT: (to explain it's not the exact duplicate of other question)

In the other question, the application connects with the service asynchronously and expect it to behave synchronously as it mentioned "By adding all kinds of Log.xx I found that the code after if(bindService(...)) actually goes BEFORE ServiceConnection.onServiceConnected is being called".

However, I'm already aware of this and the question is about if at all there is a way to connect with the service synchronously.

pree
  • 2,297
  • 6
  • 37
  • 55
  • possible duplicate of [Android how do I wait until a service is actually connected?](http://stackoverflow.com/questions/3055599/android-how-do-i-wait-until-a-service-is-actually-connected) – Sam Jun 19 '15 at 08:46

1 Answers1

5

It's a VERY BAD IDEA to block Android's main thread. This will freeze the ENTIRE phone's UI.

If you're trying to prevent the user from doing anything between starting the binding and the connection succeeding, you could throw up a non-cancelable dialog, but it would only be up for a matter of milliseconds.

If your UI has a hard dependency on the Service being present, well, you should redesign so it isn't. By their design and nature, Services are asynchronous (from the UI) and transient. The system could kill them at any time to get more resources (though it probably won't). If it's a soft dependency (like an audio streaming app) you should just hide/change UI elements until the service binds.

So even if you find a way to do a synchronous bind, I ask you as an Android user and Android developer, please find a better way.

dandc87
  • 1,096
  • 5
  • 7
  • 4
    I've seen a lot of comments like this about redesigning UI's around the way that binding works - showing progress dialogs, disabling buttons, etc. That's reasonable but it's also based on trivial examples (e.g., media players) where user interaction is the only thing that would cause you to make a service call. There are certainly cases where a service call needs to be made before continuing. – spaaarky21 Jan 07 '14 at 17:22