0

There seems to be a small delay after I bind to a remote service. The onServiceConnection works fine. The binding work fine. There is just a delay problem I think. For example in the onCreate() method of my activity if for instance say:

  • Note the mConnection onServiceConnect() sets mRemoteServiceStub correctly.

    bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE); mRemoteServiceStub.doThis();

If I run this I get an error that says that no connection yet exists. However if I put the mRemoteServiceStub.doThis() in a method to a onClickListener it work perfect. So either the connection is not made until the activities onCreate has finished running or there is a delay on the connection being made.

Does anybody know ?

And is there way to delay the application from running until the connection is made this way I can use the connection right away with no trigger. (My implementation is correct)

Onik
  • 19,396
  • 14
  • 68
  • 91
jjNford
  • 5,170
  • 7
  • 40
  • 64

1 Answers1

2

If I run this I get an error that says that no connection yet exists.

bindService() is not synchronous. You cannot use your stub until onServiceConnected() is called on your ServiceConnection object and you create the client-side proxy from the IBinder.

And is there way to delay the application from running until the connection is made this way I can use the connection right away with no trigger.

Put your binder-dependent logic in onServiceConnected().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Okay so it no being synchronous answers my first part of the question. I cannot put my binder-dependent logic inside of the onServiceConnected() method however. I should have specified this fact. I am creating an in house application development kit and the api handles all the IPC stuff so users of it can call binder dependent things as they need them. Thinking about this I could handle it in the API (create the delay before returning back to the calling apk). Thanks. – jjNford Sep 28 '11 at 20:51
  • @jjNford: "Thinking about this I could handle it in the API (create the delay before returning back to the calling apk)" -- that is unlikely to work, as `onServiceConnected()` is called on the main application thread, so your "delay" will simply block the event. Any "in house application development kit" has to support an event-driven model, for user input and anything else that happens asynchronously. Your service being bound is simply another event. Or, get rid of the service, or switch to the command pattern (i.e., sending work via `startService()`). – CommonsWare Sep 28 '11 at 20:55
  • You are right that did not work. The deal is that the service is running before the needed .apk. The service will be used in multiple apk's and runs remotely. I want to be able to bind to it and then call methods in it from my apk. I'm lost on how to solve this problem... – jjNford Sep 28 '11 at 22:27
  • @jjNford: Redesign your "in house application development kit" to be event-driven, and put your binder-dependent logic in `onServiceConnected()`. – CommonsWare Sep 28 '11 at 22:47
  • I get that. It works if I want to to be event driven. All I have to do is make a button and everything works. I would prefer that when the apk launches I can bind and call methods that register data right away with the service, no driving event. So I get what you are saying. I do not want to us the onServiceConnected either for flexibility reasons that go beyond the scope of this questions. Just seeing if there is a way that I can delay until a known connection has been established... that is all. – jjNford Sep 28 '11 at 22:52
  • 1
    it's a burden to not be able to put things such as "refresh" logic in `onResume()` where it normally lives. the fact that binding must be async and is always called back on the main thread means one has to refactor their app around binding to the service, in stead of just using it naturally. too bad. – Jeffrey Blattman Jul 09 '13 at 00:00