Search code examples
androidandroid-serviceaidl

Android AIDL: How to get service instance in activity when service is in another process?


I have a service in my app which binds to the activity. The service however is in another process and I can't figure out how to get its instance in the activity. For services in same process I do it something like this

Inside onServiceConnected

public void onServiceConnected(ComponentName name, IBinder service) {
    Service.Binder binder = (MService.Binder) binder;
    //get service
    musicSrv = binder.getService();

In my Service class I have

public class Binder extends Binder {
        public Service getService() {
            return Service.this;
        }
    }

I have no clue how to do this in my case. I couldn't find any such examples online. Can someone please help me. Thanks !!!


Solution

  • You can't! It's not possible. Actually, that is the definition of "in another process", it means the instances can never have direct access to each other.

    You can however define a inter-process communication for it using AIDL. Please check the AIDL guide on how to implement it

    https://developer.android.com/guide/components/aidl.html