Android进程系统:AIDL
【摘要】 Android系统应用框架篇:AIDL
文章目录
在介绍AIDL的原理之前先写一个简单的Demo。
举例
1 定义一个AIDL文件
package com.guoxiaoxing.android.framework.demo.system.aidl;
// Declare any non-default types here with import stat...
Android系统应用框架篇:AIDL
文章目录
在介绍AIDL的原理之前先写一个简单的Demo。
举例
1 定义一个AIDL文件
package com.guoxiaoxing.android.framework.demo.system.aidl;
// Declare any non-default types here with import statements
interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); String getName();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
rebuild一下,它会自动生成一个Java接口,它内部会生成一个抽象类Stub,它本质上是一个Binder,它可以用来进行跨进程通信。如下所示:
public interface IMyAidlInterface extends android.os.IInterface
{
//Stub类实现,它继承于Binder,同样也实现了IMyAidlInterface接口,读取Proxy传递过来的参数,并写入返回给Proxy的值。
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface
{
//接口的包名
private static final java.lang.String DESCRIPTOR = "com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Proxy类本身是私有的,为了防止外部对它进行修改,通过该方法返回Proxy对象,供外部调用。
*
* Cast an IBinder object into an com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface interface,
* generating a proxy if needed.
*/
public static com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface))) {
return ((com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface)iin);
}
return new com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface.Stub.Proxy(obj);
}
@Override 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_basicTypes:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
long _arg1;
_arg1 = data.readLong();
boolean _arg2;
_arg2 = (0!=data.readInt());
float _arg3;
_arg3 = data.readFloat();
double _arg4;
_arg4 = data.readDouble();
java.lang.String _arg5;
_arg5 = data.readString();
this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
reply.writeNoException();
return true;
}
case TRANSACTION_getName:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.getName();
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
//Proxy类,它实现了我们定义的IMyAidlInterface接口,写入传递给Stub的参数,读取Stub返回的值。
private static class Proxy implements com.guoxiaoxing.android.framework.demo.system.aidl.IMyAidlInterface
{
private android.os.IBinder mRemote;
//构造方法中传入远程Binder
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
//返回远程Binder
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
/** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */
@Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(anInt);
_data.writeLong(aLong);
_data.writeInt(((aBoolean)?(1):(0)));
_data.writeFloat(aFloat);
_data.writeDouble(aDouble);
_data.writeString(aString);
mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
@Override public java.lang.String getName() 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_getName, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
/** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException;
public java.lang.String getName() throws android.os.RemoteException;
- 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
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
2 定义一个Service组件,我们给它指定一个新的process属性,让它运行在新的进程中(因为我们要用AIDL做跨进程调用),然后
将IMyAidlInterface.Stub指定给Service组件。
public class AidlService extends Service { public AidlService() { } @Override public IBinder onBind(Intent intent) { return new AidlBinder(); } private class AidlBinder extends IMyAidlInterface.Stub{ @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public String getName() throws RemoteException { return "hello AIDL"; } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
3 定义一个Activity组件,绑定AidlService,并调用它的远程方法。
public class AidlActivity extends AppCompatActivity { private IMyAidlInterface iMyAidlInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_aidl); Intent intent = new Intent(AidlActivity.this, AidlService.class); bindService(intent, new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } }, Context.BIND_AUTO_CREATE); findViewById(R.id.btn_message).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { Toast.makeText(v.getContext(), iMyAidlInterface.getName(), Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } ; } }); }
}
- 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
以上就是AIDL调用的大致流程。
好了,我们针对上面的流程来总结一下AIDL。
文章来源: xuesong.blog.csdn.net,作者:内核笔记,版权归原作者所有,如需转载,请联系作者。
原文链接:xuesong.blog.csdn.net/article/details/81713073
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)