[android-developers] When exactly do you need to handle DeadObjectException with services?

2011-04-30 Thread MarcoAndroid
Hi there,

In the Additional Notes section here:
http://www.devdiv.com/android/docs/guide/topics/fundamentals/bound-services.html#Binding
it says: You should always trap DeadObjectException exceptions, which
are thrown when the connection has broken. This is the only exception
thrown by remote methods.

1) Does that mean that for all calls of the client to the service's
methods you need to try/catch the DeadObjectException? Even
getService() in onServiceConnected() can throw it?

2) If my service only uses a local binder (LocalBinder in the example
from the above link), so it is private to my own application and runs
in the same process as the client, can I then still get
DeadObjectException? I wonder because I see with this type of binding
that the service does *not* appear in the services overview of a
device or emulator, which makes sense to me because it is tightly
connected to the activity that bound to it. That to me means you (nor
the Android runtime) can end the service on its own, so never a chance
on DeadObjectException... True?

Thanks!
Marco


-- 
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


Re: [android-developers] When exactly do you need to handle DeadObjectException with services?

2011-04-30 Thread Dianne Hackborn
On Sat, Apr 30, 2011 at 4:05 PM, MarcoAndroid marco...@gmail.com wrote:

 1) Does that mean that for all calls of the client to the service's
 methods you need to try/catch the DeadObjectException? Even
 getService() in onServiceConnected() can throw it?


You need to catch them wherever they can be thrown.  You actually don't need
to worry about this, because this is a checked exception so if you forget to
check it somewhere you will get a compile time error.


 2) If my service only uses a local binder (LocalBinder in the example
 from the above link), so it is private to my own application and runs
 in the same process as the client, can I then still get
 DeadObjectException? I wonder because I see with this type of binding
 that the service does *not* appear in the services overview of a
 device or emulator, which makes sense to me because it is tightly
 connected to the activity that bound to it. That to me means you (nor
 the Android runtime) can end the service on its own, so never a chance
 on DeadObjectException... True?


No you can't get it, because it is in the same process, so if the service's
process goes away then your client code's process has gone away as well and
never could have gotten a result.

However because the interface generated by aidl can be used with a remote
object, at compile time there is no way to know whether or not this will be
the case, so the interface methods must declare they throw RemoteException
and you thus must handle if if calling those methods.

One way to get around this is when implementing those methods in the service
do not declare them as throwing RemoteException and on the client side cast
the interface to this implementation class.  Now the compiler knows that a
call to the method will never throw that exception.

Or just catch the exception and do nothing, or print a quick log, or
whatever.

-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
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