[Android] inter-process communication IPC - Binder

Binder is a cross-process communication in Android, bindService time, the server returns Binder object, you can get data from the server through the client object.
In the inter-process communication IPC - AIDL created in ICustomAidlInterface.aidl.
The following are ICustomAidlInterface.aidl generated according ICustomAidlInterface.Java interface class.

public interface ICustomAidlInterface extends android.os.IInterface {
/**
* Local-side IPC implementation stub class.
*/
public static abstract class Stub extends android.os.Binder implements com.sjl.exercise.ICustomAidlInterface {
private static final java.lang.String DESCRIPTOR = "com.sjl.exercise.ICustomAidlInterface";

/**
* Construct the stub at attach it to the interface.
*/
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}

/**
* Cast an IBinder object into an com.sjl.exercise.ICustomAidlInterface interface,
* generating a proxy if needed.
*/
public static com.sjl.exercise.ICustomAidlInterface asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
//④
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.sjl.exercise.ICustomAidlInterface))) {
return ((com.sjl.exercise.ICustomAidlInterface) iin);
}
return new com.sjl.exercise.ICustomAidlInterface.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 {
//③
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(descriptor);
return true;
}
case TRANSACTION_getCurrentTime: {
data.enforceInterface(descriptor);
java.lang.String _result = this.getCurrentTime();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_insertUser: {
data.enforceInterface(descriptor);
com.sjl.exercise.bean.UserBean _arg0;
if ((0 != data.readInt())) {
_arg0 = com.sjl.exercise.bean.UserBean.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.insertUser(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_getUsers: {
data.enforceInterface(descriptor);
java.util.List<com.sjl.exercise.bean.UserBean> _result = this.getUsers();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_clearUser: {
data.enforceInterface(descriptor);
this.clearUser();
reply.writeNoException();
return true;
}
default: {
return super.onTransact(code, data, reply, flags);
}
}
}

private static class Proxy implements com.sjl.exercise.ICustomAidlInterface {
private android.os.IBinder mRemote;

Proxy(android.os.IBinder remote) {
mRemote = remote;
}

@Override
public android.os.IBinder asBinder() {
return mRemote;
}

public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
}

@Override
public java.lang.String getCurrentTime() 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_getCurrentTime, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}

@Override
public void insertUser(com.sjl.exercise.bean.UserBean userBean) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((userBean != null)) {
_data.writeInt(1);
userBean.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_insertUser, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}

@Override
public java.util.List<com.sjl.exercise.bean.UserBean> getUsers() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<com.sjl.exercise.bean.UserBean> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getUsers, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.sjl.exercise.bean.UserBean.CREATOR);
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}

@Override
public void clearUser() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_clearUser, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
//②
static final int TRANSACTION_getCurrentTime = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_insertUser = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_getUsers = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_clearUser = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
}
//①
public java.lang.String getCurrentTime() throws android.os.RemoteException;

public void insertUser(com.sjl.exercise.bean.UserBean userBean) throws android.os.RemoteException;

public java.util.List<com.sjl.exercise.bean.UserBean> getUsers() throws android.os.RemoteException;

public void clearUser() 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 is
165
166
167 is
168
169
170.
171 is
172
173
174
175
176
177
178
179
180 [
181
182
ICustomAidlInterface lowermost class is one of several methods ① aidl file corresponding to the statement, the statement ② where int value of the corresponding method, in abstract method Stub # onTransact method can be seen at ③ it is with the corresponding method int value ② declared distinguish invoked.

public interface ICustomAidlInterface extends android.os.IInterface {

public static abstract class Stub extends android.os.Binder implements com.sjl.exercise.ICustomAidlInterface {
private static final java.lang.String DESCRIPTOR = "com.sjl.exercise.ICustomAidlInterface";

...
@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
//③
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(descriptor);
return true;
}
case TRANSACTION_getCurrentTime: {
data.enforceInterface(descriptor);
java.lang.String _result = this.getCurrentTime();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_insertUser: {
data.enforceInterface(descriptor);
com.sjl.exercise.bean.UserBean _arg0;
if ((0 != data.readInt())) {
_arg0 = com.sjl.exercise.bean.UserBean.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.insertUser(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_getUsers: {
data.enforceInterface(descriptor);
java.util.List<com.sjl.exercise.bean.UserBean> _result = this.getUsers();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_clearUser: {
data.enforceInterface(descriptor);
this.clearUser();
reply.writeNoException();
return true;
}
default: {
return super.onTransact(code, data, reply, flags);
}
}
}

private static class Proxy implements com.sjl.exercise.ICustomAidlInterface {
...
//②
static final int TRANSACTION_getCurrentTime = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_insertUser = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_getUsers = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_clearUser = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
}
//①
public java.lang.String getCurrentTime() throws android.os.RemoteException;

public void insertUser(com.sjl.exercise.bean.UserBean userBean) throws android.os.RemoteException;

public java.util.List<com.sjl.exercise.bean.UserBean> getUsers() throws android.os.RemoteException;

public void clearUser() 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 is
54 is
55
56 is
57 is
58
59
60
61 is
62 is
63 is
64
65
66
67
68
69
70
the Stub constructor passed using ICustomAidlInterface.Stub.asInterface (service) when IInterface method and DESCRIPTOR, the client obtains to an interface by binding service aidl in Stub ④ at Stub # asInterface method static abstract class can be seen using Binder # queryLocalInterface method to get local aidl interface is returned if there is a local found aidl interfaces, if not defined returns proxy proxy class (queryLocalInterface way is than to confirm whether to return to DESCRIPTOR corresponding IInterface, unable to find the corresponding IInterface in the different processes and can only be carried out by proxy class method calls aidl proxy).

public interface ICustomAidlInterface extends android.os.IInterface {
/**
* Local-side IPC implementation stub class.
*/
public static abstract class Stub extends android.os.Binder implements com.sjl.exercise.ICustomAidlInterface {
private static final java.lang.String DESCRIPTOR = "com.sjl.exercise.ICustomAidlInterface";
...
public static com.sjl.exercise.ICustomAidlInterface asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
//④
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.sjl.exercise.ICustomAidlInterface))) {
return ((com.sjl.exercise.ICustomAidlInterface) iin);
}
return new com.sjl.exercise.ICustomAidlInterface.Stub.Proxy(obj);
}

Proxy com.sjl.exercise.ICustomAidlInterface the implements static class Private {
...
}
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
View Proxy class can see it the IPC mRemote are implemented, and the corresponding methods are used mRemote.transact method, int value passed to the method corresponding to the client and pass data parameters returned from the server reply results

private static class Proxy implements com.sjl.exercise.ICustomAidlInterface {
private android.os.IBinder mRemote;

Proxy(android.os.IBinder remote) {
mRemote = remote;
}

@Override
public android.os.IBinder asBinder() {
return mRemote;
}

public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
}

@Override
public java.lang.String getCurrentTime() 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_getCurrentTime, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}

}

}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
Binder # Transact code below, eventually called Binder # onTransact , that is, Binder # onTransact

/**
* Default implementation rewinds the parcels and calls onTransact. On
* the remote side, transact calls into the binder to do the IPC.
*/
public final boolean transact(int code, @NonNull Parcel data, @Nullable Parcel reply,
int flags) throws RemoteException {
if (false) Log.v("Binder", "Transact: " + code + " to " + this);

IF (Data = null!) {
data.setDataPosition (0);
}
Boolean = R & lt onTransact (code, Data, Reply, the flags);
IF (= Reply null!) {
reply.setDataPosition (0);
}
return R & lt;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
Binder # onTransact follows, corresponding to the code determined by the method of calling, data acquisition parameters passed, reply return value is set

@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
case TRANSACTION_insertUser: {
data.enforceInterface(descriptor);
com.sjl.exercise.bean.UserBean _arg0;
if ((0 != data.readInt())) {
_arg0 = com.sjl.exercise.bean.UserBean.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.insertUser(_arg0);
reply.writeNoException(http://www.my516.com);
return true;
}
case TRANSACTION_getUsers: {
data.enforceInterface(descriptor);
java.util.List <com.sjl.exercise.bean.UserBean> _result this.getUsers = ();
reply.writeNoException ();
reply.writeTypedList (_result);
return to true;
}
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
summary:
service: the server returns the object that implements IBinder interface;
client: If the client found aidl instance locally directly into use, or by the Proxy agent call;
communication: cast local client can call call the method, the client needs through other processes proxy agent calling the method;
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/11014139.html