cuongptnk cuongptnk - 17 days ago 6
Android Question

Understand Bound Service Documentation

Refer to the example in this Bound Service documentation: https://developer.android.com/guide/components/bound-services.html

Binder is a class implementing the interface IBinder. However, in the example, I cannot understand why we use

IBinder mBinder
instead of
Binder mBinder
? Why make it more complicated with IBinder? And why do we need LocalBinder class for just to return the instance of LocalService?

Answer

You don't need to do it that way. If you want to store a LocalBinder, you can. If you want to return a new Binder instance for each client binding, you can. The only thing you need to do is return non-null if you want it to be possible to bind to you (if you want the service to only work via startService and not via bindService, you can just return null).

Comments