0

I created sample local service project as provided in Android Service article Extending the Binder class http://developer.android.com/guide/topics/fundamentals/bound-services.html.

Everything works fine. However the query is

With unbind(), service doesn't get stopped. Should i need to use stopSelf() explicitly if yes than what does unbind() does.

I have create sample project with three buttons -> start, stop and fetch

start --> to bind service

stop --> to unbind service

start --> to access method of service. (random number generation method)

Now access to:

  1. start - binds the service

  2. fetch - gets access to method of service

  3. stop - unbinds the service

Here i excepted that service will be unbind and destroyed. However

Step 4: fetch - this method still works and generates random number whereas expected was some sort of exception or may be force close.

(onServiceDisconnected will not be called: since it will be called only in case of crash)

Ravibhushan
  • 171
  • 3
  • 14

1 Answers1

1

For a remote service -- where the service you bind to is in another process -- attempting to call a method on the binder proxy after unbinding should result in a RemoteException.

For a local service -- where the service is still in your process -- attempting to call a method on the binder after unbinding may still work. The binder in this case is the very same object that your service returned via onBind(). Garbage collection rules state that if you hold onto the binder, the binder cannot be garbage collected.

However, calling methods on a binder after unbinding is dangerous and poor form. The service thinks it was destroyed. If there is anything your binder depends upon that the service cleaned up in onDestroy(), that might cause a crash. Similarly, you are leaking the service's memory, until you allow the binder to be garbage collected.

BTW, I hope your service is doing something more meaningful than "generates random number". You do not need a service for that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your valuable information. Regarding random number generation :) as already mentioned in my query; it is a sample project to study service. – Ravibhushan Mar 14 '12 at 10:52