当前位置:操作系统 > 安卓/Android >>

Android实现进程间通信aidl的使用

现在总结一下这个用法:
其实很简单,主要就是以下的几个步骤:
1.第一步,定义aidl文件(可以参照IWifiManager.java的定义来实现,其实就是定义一个Inte易做图ce,提供被调用的方法);
例如 (IMyService.aidl):
package com.demo;

import com.demo.Person;

inte易做图ce IMyService {
        void savePersonInfo(in Person person);//注意,这里的Person类不是基本数据类型哦,要进行处理的,见下文
        List<Person> getAllPerson();
}


2.第二步,写一个service,实现stub类(也就是实现刚才的那些接口),return stub类的对象(也就是binder);
    创建一个类实现刚才那个aidl的接口:
public class RemoteService extends Service {

        private LinkedList<Person> personList = new LinkedList<Person>();
        
        @Override
        public IBinder onBind(Intent intent) { //activity
                return mBinder;
        }

        private final IMyService.Stub mBinder = new IMyService.Stub(){

                @Override
                public void savePersonInfo(Person person) throws RemoteException {
                        if (person != null){
                                personList.add(person);
                        }
                }

                @Override
                public List<Person> getAllPerson() throws RemoteException {
                        return personList;
                }
        };
}


注:
  这里会看到有一个名为IMyService.Stub类,查看aidl文件生成的Java文件源代码就能发现有这么一段代码:
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.demo.IMyService
 原来Stub类就是继承于Binder类,也就是说RemoteService类和普通的Service类没什么不同,只是所返回的IBinder对象比较特别,是一个实现了AIDL接口的Binder。

 


3.第三步,在activity里面启动service。
通过IMyService.Stub.asInte易做图ce(service)来得到IMyService对象:
private IMyService mRemoteService;

private ServiceConnection mRemoteConnection = new ServiceConnection() {   
        public void onServiceConnected(ComponentName className, IBinder service) {   
                mRemoteService = IMyService.Stub.asInte易做图ce(service);   
        }   

        public void onServiceDisconnected(ComponentName className) {   
                mRemoteService = null;   
        }   
};
注:
 在生成的IMyService.java里面会找到这样的代码:
/**
* Cast an IBinder object into an com.demo.IMyService inte易做图ce,
* generating a proxy if needed.
*/
public static com.demo.IMyService asInte易做图ce(android.os.IBinder obj) {...}

 


而service的绑定没有什么不同:
if (mIsRemoteBound) {
        unbindService(mRemoteConnection);
}else{
        bindService(new Intent("com.demo.IMyService"),
                               mRemoteConnection, Context.BIND_AUTO_CREATE);
}

mIsRemoteBound = !mIsRemoteBound;
 
通过IPC调用/传递数据
    客户端绑定service后就能通过IPC来调用/传递数据了,直接调用service对象的接口方法:

addPersonButton.setOnClickListener(
                new View.OnClickListener(){
                        private int index = 0;

                        @Override
                        public void onClick(View view) {
                                Person person = new Person();
                                index = index + 1;
                                person.setName("Person" + index);
                                person.setAge(20);
                                person.setTelNumber("123456");
                                try {
                                        mRemoteService.savePersonInfo(person);
                                } catch (RemoteException e) {
                               

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,