[转载]Android Service学习之AIDL实例 - 梅义 - 博客园

[转载]Android Service学习之AIDL实例 – 梅义 – 博客园.

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

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

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

Demo运行效果图

Aidl文件
Person.aidl

1
2
package com.archermind.aidl;
parcelable Person;

ITaskBinder.aidl

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
package com.archermind.aidl;
interface ITaskCallBack {
    void onActionBack(String str);
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@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;
        }
    };

客户端实现回调接口

1
2
3
4
5
6
7
8
9
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);
        }
        
    };

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏