注:本文是我早些时候在eoe上发布的一篇文章

由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象。在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的基本单元,并且有序的通过进程边界。

以下讲对如何实现AIDL调用进行讲解,本文包括AIDL接口调用,回调,以及传递复杂数据类型

Demo运行效果图

Aidl文件
Person.aidl

package com.archermind.aidl;
parcelable Person;



ITaskBinder.aidl

package com.archermind.aidl;
import com.archermind.aidl.ITaskCallBack;
import com.archermind.aidl.Person;
interface ITaskBinder {

   void fuc01();
   void fuc02();
   String fuc03(in Person person);
   void registerCallBack(ITaskCallBack cb);
   void unregisterCallBack();
}

  



ITaskCallBack.aidl

package com.archermind.aidl;

interface ITaskCallBack {
    void onActionBack(String str);
}

  



先看Myservice的代码
服务端实现了AIDL的相关接口,供客户端调用

@Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return mBinder;
    }
    
    private final ITaskBinder.Stub mBinder=new ITaskBinder.Stub() {
        
        @Override
        public void unregisterCallBack() throws RemoteException {
            // TODO Auto-generated method stub
            Log.v(TAG,"unregisterCallBack...");
            mTaskCallBack=null;
        }
        
        @Override
        public void registerCallBack(ITaskCallBack cb) throws RemoteException {
            // TODO Auto-generated method stub
            Log.v(TAG,"registerCallBack...");
            mTaskCallBack=cb;
        }
        
        @Override
        public void fuc01() throws RemoteException {
            // TODO Auto-generated method stub
            Log.v(TAG,"fuc01...");
        }

        @Override
        public void fuc02() throws RemoteException {
            // TODO Auto-generated method stub
            Log.v(TAG,"fuc02...");
            mTaskCallBack.onActionBack("hello world");
        }

        @Override
        public String fuc03(Person person) throws RemoteException {
            // TODO Auto-generated method stub
            String name=person.getName();
            String descrip=person.getDescrip();
            int sex=person.getSex();
            String ret="";
            if(sex==0){
                ret="Hello "+name+",you are handsome"+"("+descrip+")";
            }else{
                ret="Hello "+name+",you are beautiful"+"("+descrip+")";
            }
            return ret;
        }
    };

  



客户端实现回调接口

private final ITaskCallBack.Stub mCallBack=new ITaskCallBack.Stub() {

        @Override
        public void onActionBack(String str) throws RemoteException {
            // TODO Auto-generated method stub
            Log.v(TAG,"onActionBack str="+str);
        }
        
    };




客户端如何与服务器绑定并建立连接

Intent intent=new Intent("com.archermind.aidl.myservice");
bindService(intent,mConnection,BIND_AUTO_CREATE);

private ServiceConnection mConnection=new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            mService=ITaskBinder.Stub.asInterface(service);
            try {
                mService.registerCallBack(mCallBack);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.v(TAG,"onServiceConnected");
        }

  



源码工程:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NzQ0OTZ8ZjczNWI2NGN8MTM4ODY1NTQ1MnwxNjc0MnwyMDM5NTc%3D

posted on 2014-01-02 17:49  梅义  阅读(1256)  评论(0编辑  收藏  举报