Android进程间通信之AIDL

Android系统中,进程之间不能共享内存为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用Remote Procedure CallRPC)方式来实现Android系统中提供了一种接口定义语言(Interface Definition LanguageIDL)来公开服务的接口这种可以提供跨进程访问的服务称为AIDLAndroid Interface Definition Language)服务

Android系统中,服务端通过实现AIDL接口来提供相应的功能服务,客户端连接到AIDL服务,调用相应的接口来实现远程功能调用。对于服务端的功能接口,客户端都是以远程调用的方式实现,因此,不通的客户端调用服务端,调用过程都不是在一个线程中完成的。下面介绍AIDL服务端和客户端的实现方式。

先看服务端实现过程,创建Android应用AidlServer

1.定义AIDL接口。

创建AIDL文件IAidlCommInterface.aidl,定义功能接口。

interface IAidlCommInterface {
    void OnMessage(int msgId, int param1, int param2);
}

2.创建服务端服务,实现IAidlCommInterface.Stub接口。

public class TestService extends Service {
 
    private AIDLServerBinder mAidlServerBinder = null;
 
    @Override
    public IBinder onBind(Intent intent) {
        if (mAidlServerBinder == null){
            mAidlServerBinder = new AIDLServerBinder();
        }
        return mAidlServerBinder;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(" [TestService] ", "onCreate");
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(" [TestService] ", "onDestroy");
    }
 
    public class AIDLServerBinder extends IAidlCommInterface.Stub{
 
        @Override
        public void OnMessage(int msgId, int param1, int param2) throws RemoteException {
 
            Log.e(" [TestService] ", "OnMessage: " + msgId + "(" + param1 + "," + param2 + ")");
        }
    }
}

3.AndroidManifest.xml添加intent-filter

<service
            android:name=".TestService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.test.aidlserver.TestService" />
            </intent-filter>
</service>

 

客户端实现过程如下:

创建客户端应用AidlClien

1.定义AIDL接口,使用与AidlServer相同的接口定义文件IAidlCommInterface.aidl

interface IAidlCommInterface {
    void OnMessage(int msgId, int param1, int param2);
}

2.Activity中连接AIDL服务并调用相应服务接口。服务端通过bindService来绑定AIDL服务,绑定成功后会回掉onServiceConnected,在onServiceConnected中会传递远程AIDL服务的IBinder引用,通过IAidlCommInterface.Stub.asInterface来转为IAidlCommInterface接口引用。这样就可以通过该接口直接进行远程功能调用。

public class MainActivity extends AppCompatActivity {

    private IAidlCommInterface mAIDLService = null;

    private ServiceConnection mServerConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e(" [AidlClient] ", "onServiceConnected");
            mAIDLService = IAidlCommInterface.Stub.asInterface(iBinder);
            try {
                mAIDLService.OnMessage(999, 111, 222);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mAIDLService = null;
            Log.e(" [AidlClient] ", "onServiceDisconnected");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent("com.test.aidlserver.TestService");
        this.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
        Log.e(" [AidlClient] ", "onStart");
    }
}


以上的例子中,service端实现AIDL接口,并提供了系统服务,客户端通过bindService绑定到远程服务(注意,绑定时使用的intent的action必需与service端AndroidManifest.xml中对于服务的intent-filter标签中的action一致),连接成功后,客户端获取到一个远程AIDL服务接口对象,这样,客户端就可以调用服务端aidl接口

void OnMessage(int msgId, int param1, int param2);
来实现远程功能调用了。


说明:AIDL通信,由于服务端AIDL接口都是各个客户端在自己的进程空间中通过远程调用的方式完成调用,因此,远程调用过程中操作的数据需要做好互斥处理;同样,也正是由于这个特性,AIDL适合要求并发的情况。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值