Android多进程通信(1)----代码架构推荐

在实际的项目中,如果进程A和进程B进行AIDL绑定通信,通常B进程中的Service不止一个。

这时候采用将A进程和B进程的一个MainService绑定,再通过MainService获得B进程中其他service的接口。这样一来,绑定了一个MainService就可以与所有service交互。 代码的架构将清晰明了。
进程主Service的AIDL文件IMainInterface.aidl:
interface IMainInterface{
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    IBinder getFirstService();
    IBinder getSecodService();
}


进程的其他Service的AIDL文件IFirstInterface.aidl和ISecondInterface.aidl
interface IFirstInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    String getString(String s);
}

interface ISecondInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    int add(int a, int b);
}


新建每个aidl的Stub文件  MyMainStub.java  MyFirstStub.java MySecondStub.java
public class MyMainStub extends IMainInterface.Stub{

    @Override
    public IBinder getFirstService() throws RemoteException {
        return new MyFirstStub();
    }

    @Override
    public IBinder getSecodService() throws RemoteException {
        return new MySecondStub();
    }
}

public class MyFirstStub extends IFirstInterface.Stub{

    @Override
    public String getString(String s) throws RemoteException {
        return s.toUpperCase();
    }
}

public class MySecondStub extends ISecondInterface.Stub {
    @Override
    public int add(int a, int b) throws RemoteException {
        return a+b;
    }
}


新建每个Service文件 MainService.java FirstService.java SecondService.java
public class MainService extends Service {

    MyMainStub myMainStub = new MyMainStub();

    @Override
    public IBinder onBind(Intent intent) {
        return myMainStub;
    }
}

public class FirstService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

public class SecondService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

在进程的非主service当中onbind返回null即可。

在MainActivity当中使用:
serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = IMainInterface.Stub.asInterface(service);

        /*try {
            IBinder iBinder = mService.getFirstService();
            fService  = IFirstInterface.Stub.asInterface(iBinder);
            mTextView.setText(fService.getString("helloword"));
        } catch (RemoteException e) {
            e.printStackTrace();
        }*/

        try {
            IBinder iBinder = mService.getSecodService();
            sService = ISecondInterface.Stub.asInterface(iBinder);
            mTextView.setText(sService.add(5, 5) + "");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章