That code has an interesting.... thing in it:

  /** The maximum amount of time to wait (milliseconds) for a successful
binding to the service */
  private static final int CONNECTION_TIMEOUT = 60 * 1000; And then:
connectLatch.await(CONNECTION_TIMEOUT, TimeUnit.SECONDS);

So it waits 60 thousand seconds, or about 16,6 hours for the service
binding to happen. I think that's a bit excessive, unless called on a very
heavily loaded Android device.

It should be possible to follow the general idea, as long as you're careful
with what thread you're on - but that strikes me as an ugly hack.

Can you bind in onCreate and unbind in onDestroy for this particular
activity?

Also, if the service is local, and already started at some earlier time,
well, local means within the same Java VM, so you can just store the
service instance in some global somewhere.

That too would be a hack, but less ugly and prone to breakage than trying
to hammer an event-driven system into a synchronous sequence by playing
games with threads and the message loop.

-- Kostya

3 декабря 2011 г. 2:04 пользователь goosedroid <alexrhel...@gmail.com>написал:

> I was wondering if there was any technique to bind to a local service
> synchronously. Of course if the service was remote it would be an
> especially bad idea, but binding to a local service seems to happen
> quickly.
>
> The situation i often run into is that
> ServiceConnection.onServiceConnected() isn't called at a convenient
> time. For example, if you want to invoke a method on a local service
> in onActivityResult() but bind/unbind on onStart/onStop. The service
> still won't be available by the time onActivityResult() is called.
>
> I did come across this:
>
>
> http://code.google.com/p/google-cache-invalidation-api/source/browse/trunk/src/java/com/google/ipc/invalidation/external/client/android/service/ServiceBinder.java
>
> Which uses a CountdownLatch to gate the calling thread, but my
> attempts to use it have resulted in deadlock.
>
> I even tried binding to the service from another thread, hoping that
> ServiceConnection.onServiceConnected() would be called from the thread
> which called bindService, but this does not seem to be the case.
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to