AIDL sample
来源:互联网 发布:ubuntu pyqt4安装 编辑:程序博客网 时间:2024/06/05 12:41
AIDL
1.编写一个AIDL
如:
package com.elfylin; interface IMyService { String getValue();}
AIDL和java一样,要保证路径和包名一样。
不过不是每一个java对象,AIDL都支持,不需要声明的有int、boolean、String、CharSequence,在List、Map、Parcelabels也只能包含基本类型。
2.,通过ADT生成一个java文件。
如下:
package com.elfylin;public interface IMyService extends android.os.IInterface{/** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.elfylin.IMyService { private static final java.lang.String DESCRIPTOR = "com.elfylin.IMyService"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.elfylin.IMyService interface, * generating a proxy if needed. */ public static com.elfylin.IMyService asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof com.elfylin.IMyService))) { return ((com.elfylin.IMyService)iin); } return new com.elfylin.IMyService.Stub.Proxy(obj); } public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_getValue: { data.enforceInterface(DESCRIPTOR); java.lang.String _result = this.getValue(); reply.writeNoException(); reply.writeString(_result); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.elfylin.IMyService { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } public java.lang.String getValue() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.lang.String _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_getValue, _data, _reply, 0); _reply.readException(); _result = _reply.readString(); } finally { _reply.recycle(); _data.recycle(); } return _result; } } static final int TRANSACTION_getValue = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); } public java.lang.String getValue() throws android.os.RemoteException;}
里面有如下信息
a. IMyService继承IInterface,因此有asBinder这个函数
b 里面的内部类Stub
Stub继承了Binder,实现了IMyService。
实现了IInterface的asBinder(),实现了IMyService的getValue()函数。
增加了com.elfylin.IMyService asInterface(android.os.IBinder obj)
重载了Binder的 onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags)
c 内部类 Proxy
Proxy实现了com.elfylin.IMyService
里面有一个内部变量private android.os.IBinder mRemote;
增加了android.os.IBinder asBinder()
java.lang.String getInterfaceDescriptor()
实现了getValue()
3.Service实现
a 编写一个Service的Android工程作为外壳。类名MyService继承Service。
b 在里面些一个自己的类MyServiceImpl继承IMyService.Stub
重写里面的getValue,变成自己的实现。
c 重写这个Service外壳的onBinder函数,返回继承于IMyService.Stub MyServiceImpl的实例。
package com.elfylin;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException;public class MyService extends Service { public class MyServiceImpl extends IMyService.Stub { @Override public String getValue() throws RemoteException { return "'test"; } } @Override public IBinder onBind(Intent intent) { return new MyServiceImpl(); }}
4、客户端实现
a 要把服务器端的AIDL和生成的java文件一起拷贝到客户端的src目录
b 写一个ServiceConnection的回调实例,重载其
public void onServiceConnected(ComponentName name, IBinder service)
public void onServiceDisconnected(ComponentName name)
这两个回调函数
并且在public void onServiceConnected(ComponentName name, IBinder service)中
IMyService myService = IMyService.Stub.asInterface(service)
以此来获取AIDL中的proxy对象
private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { myService = IMyService.Stub.asInterface(service); btnInvokeAIDLService.setEnabled(true); } @Override public void onServiceDisconnected(ComponentName name) { } };
c 在onCreate或者其他事件触发中,连接远程服务,并且传入刚才的回调
bindService(new Intent("com.elfy.aidl.IMyService"), serviceConnection
d 在所有需要服务的地方通过myService来调用即可
bindService(new Intent("net.blogjava.mobile.aidl.IMyService"), serviceConnection, Context.BIND_AUTO_CREATE);
call process:
client:bindService(new Intent,serviceConnection).
service:IBinder onBinder(Intent intent)
return MyServiceImpl
call ServiceConnection onServiceConnected(MyServiceImpl);
client:
onServiceConnected(service)
myService = IMyservice.Stub.asInterface(service)
IMyservice.Stub.asInterface
return new com.elfylin.IMyService.Stub.Proxy(obj)
myService = com.elfylin.IMyService.Stub.Proxy
myService.getValue
com.elfylin.IMyService.Stub.Proxy.getValue
mRemote.transact(Stub.TRANSACTION_getValue, _data, _reply, 0);
service.transact(Stub.TRANSACTION_getValue, _data, _reply, 0)
server
MyServiceImpl.onTransact
IMyService.Stub.onTransact
IMyService.Stub.getValue
MyServiceImpl.getValue
reply.writeString(_result);
client
_result = _reply.readString();
- AIDL sample
- sample
- !!!sample
- sample
- AIDL
- aidl
- AIDL
- AIDL
- AIDL
- AIDL
- AIDL
- aidl
- aidl
- AIDL
- AIDL
- aidl
- AIDL
- AIDL
- mysql sql语句大全
- 系统闹钟程序知识点记录
- 套接字编程中,select:bad file descriptor
- Linux清除正在写的文件内容(比如nohup.out)
- HDU 1712 ACboy needs your help 多组背包
- AIDL sample
- PHP的三目运算符
- SQL Server 2008 数据维护实务
- 几个有用的iOS开源代码
- OCX和DLL的区别
- 文件描述符、文件表项、V节点表项的一些总结
- 28个Unix/Linux的命令行神器
- php_server详解
- 一步一步复习数据结构和算法基础-KMP算法