
欢迎转载,转载请注明:http://blog.****.net/zhgxhuaa
一个Binder实例
我们Binder的学习将从以下的一个实例開始。依据Android文档中的描写叙述,创建一个Binder服务主要包含例如以下3步:
以下详细看一下在eclipse中是怎样开发一个Binder应用的。
第一步:在project文件夹下定义aidl文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.zxh.ipc;
import com.zxh.ipc.PermissionInfo;
interface ITestManager{
int checkPermission(String
int checkUidPermission(String int uid);
boolean addPermission(in
void removePermission(String
boolean isProtectedBroadcast(String
}
|
第二步:在定义aidl后,eclipse在project文件夹的gen文件夹下会自己主动生成相应的接口,以下是有aidl自己主动生成的Java代码:
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
package com.zxh.ipc;
public interface ITestManager extends android.os.IInterface
{
/**
public static abstract class Stub extends android.os.Binder implements com.zxh.ipc.ITestManager
{
private static final java.lang.String "com.zxh.ipc.ITestManager" ;
/**
public Stub()
{
this .attachInterface( this ,
}
/**
*
*/
public static com.zxh.ipc.ITestManager
{
if ((obj null ))
return null ;
}
android.os.IInterface
if (((iin null ) instanceof com.zxh.ipc.ITestManager)))
return ((com.zxh.ipc.ITestManager)
}
return new com.zxh.ipc.ITestManager.Stub.Proxy(obj);
}
@Override
public android.os.IBinder
{
return this ;
}
@Override
public boolean onTransact( int code, int flags)
throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
reply.writeString(DESCRIPTOR);
return true ;
}
case TRANSACTION_checkPermission:
data.enforceInterface(DESCRIPTOR);
java.lang.String
_arg0
java.lang.String
_arg1
int _result this .checkPermission(_arg0,
reply.writeNoException();
reply.writeInt(_result);
return true ;
}
case TRANSACTION_checkUidPermission:
data.enforceInterface(DESCRIPTOR);
java.lang.String
_arg0
int _arg1;
_arg1
int _result this .checkUidPermission(_arg0,
reply.writeNoException();
reply.writeInt(_result);
return true ;
}
case TRANSACTION_addPermission:
data.enforceInterface(DESCRIPTOR);
com.zxh.ipc.PermissionInfo
if (( 0 !=
_arg0
}
else {
_arg0 null ;
}
boolean _result this .addPermission(_arg0);
reply.writeNoException();
reply.writeInt(((_result) 1 ) 0 )));
return true ;
}
case TRANSACTION_removePermission:
data.enforceInterface(DESCRIPTOR);
java.lang.String
_arg0
this .removePermission(_arg0);
reply.writeNoException();
return true ;
}
case TRANSACTION_isProtectedBroadcast:
data.enforceInterface(DESCRIPTOR);
java.lang.String
_arg0
boolean _result this .isProtectedBroadcast(_arg0);
reply.writeNoException();
reply.writeInt(((_result) 1 ) 0 )));
return true ;
}
}
return super .onTransact(code,
}
private static class Proxy implements com.zxh.ipc.ITestManager
{
private android.os.IBinder
Proxy(android.os.IBinder
{
mRemote
}
@Override
public android.os.IBinder
{
return mRemote;
}
public java.lang.String
{
return DESCRIPTOR;
}
@Override
public int checkPermission(java.lang.String
throws android.os.RemoteException
{
android.os.Parcel
android.os.Parcel
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(permName);
_data.writeString(pkgName);
mRemote.transact(Stub.TRANSACTION_checkPermission, 0 );
_reply.readException();
_result
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public int checkUidPermission(java.lang.String int uid) throws android.os.RemoteException
{
android.os.Parcel
android.os.Parcel
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(permName);
_data.writeInt(uid);
mRemote.transact(Stub.TRANSACTION_checkUidPermission, 0 );
_reply.readException();
_result
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public boolean addPermission(com.zxh.ipc.PermissionInfo throws android.os.RemoteException
{
android.os.Parcel
android.os.Parcel
boolean _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((info null ))
_data.writeInt( 1 );
info.writeToParcel(_data, 0 );
}
else {
_data.writeInt( 0 );
}
mRemote.transact(Stub.TRANSACTION_addPermission, 0 );
_reply.readException();
_result 0 !=
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public void removePermission(java.lang.String throws android.os.RemoteException
{
android.os.Parcel
android.os.Parcel
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(name);
mRemote.transact(Stub.TRANSACTION_removePermission, 0 );
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
@Override
public boolean isProtectedBroadcast(java.lang.String throws android.os.RemoteException
{
android.os.Parcel
android.os.Parcel
boolean _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(actionName);
mRemote.transact(Stub.TRANSACTION_isProtectedBroadcast, 0 );
_reply.readException();
_result 0 !=
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_checkPermission 0 );
static final int TRANSACTION_checkUidPermission 1 );
static final int TRANSACTION_addPermission 2 );
static final int TRANSACTION_removePermission 3 );
static final int TRANSACTION_isProtectedBroadcast 4 );
}
public int checkPermission(java.lang.String throws android.os.RemoteException;
public int checkUidPermission(java.lang.String int uid) throws android.os.RemoteException;
public boolean addPermission(com.zxh.ipc.PermissionInfo throws android.os.RemoteException;
public void removePermission(java.lang.String throws android.os.RemoteException;
public boolean isProtectedBroadcast(java.lang.String throws android.os.RemoteException;
}
|
第三步:继承上一步生成的接口,实现自己的Service类:
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
|
package com.zxh.server;
import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.zxh.ipc.ITestManager;
import com.zxh.ipc.PermissionInfo;
public class TestService extends Service{
private final static String "zxh" ;
class TestManagerService extends ITestManager.Stub{
@Override
public int checkPermission(String
throws RemoteException
Log.i(TAG, "checkPermission );
return 0 ;
}
@Override
public int checkUidPermission(String int uid)
throws RemoteException
Log.i(TAG, "checkUidPermission );
return 0 ;
}
@Override
public boolean addPermission(PermissionInfo throws RemoteException
Log.i(TAG, "addPermission );
return false ;
}
@Override
public void removePermission(String throws RemoteException
Log.i(TAG, "removePermission );
}
@Override
public boolean isProtectedBroadcast(String
throws RemoteException
Log.i(TAG, "isProtectedBroadcast );
return false ;
}
}
private TestManagerService new TestManagerService();
@Override
public IBinder
Log.i( "zxh" , "unbindService" );
return mService;
}
@Override
public void unbindService(ServiceConnection
super .unbindService(conn);
Log.i( "zxh" , "unbindService" );
}
}
|
第四步:实现client代码:
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
|
package com.zxh.server;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import com.zxh.ipc.ITestManager;
public class MainActivity extends Activity
private ITestManager null ;
ServiceConnection new ServiceConnection()
@Override
public void onServiceDisconnected(ComponentName
Log.i( "binder" , "onServiceDisconnected" );
}
@Override
public void onServiceConnected(ComponentName
manager
}
};
@Override
protected void onCreate(Bundle
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.startServer).setOnClickListener( new OnClickListener()
@Override
public void onClick(View
bindService( new Intent(MainActivity. this , class ),
}
});
findViewById(R.id.stop).setOnClickListener( new OnClickListener()
@Override
public void onClick(View
unbindService(connection);
}
});
findViewById(R.id.check).setOnClickListener( new OnClickListener()
@Override
public void onClick(View
try {
manager.checkPermission( "permName" , "pkgName" );
} catch (RemoteException
e.printStackTrace();
}
}
});
}
}
|
到这里我们就实现了一个完整的Binder实例,以下是project文件夹的截图:
完整代码能够查看后面的附件。以下来介绍一下这个实例中涉及到的一些知识点。
AIDL
除了eclipse,也能够使用Android开发包中提供的aidl命令工具来生成,例如以下是adil命令:
关于adil的具体介绍能够查看Android官方文档,地址为:http://developer.android.com/intl/zh-cn/guide/components/aidl.html
想要了解aidl处理过程的朋友也能够自己查看aidl的源码,位于@/frameworks/base/tools/adil。
AIDL编译产生的类
以下我们来分析一下aidl编译产生的ITestManager类。
从ITestManager中能够看出,ITestManager实现了IInterface接口。在ITestManager类*包括了三部分内容,各自是:
我们在ITestManager.adil中定义的接口。
Stub类:主要由Server端实现,Server端通过继承本类,来提供详细的处理逻辑。
Proxy类:client代理,通过mRemote成员变量訪问远程Binder服务。
为了实现我们在ITestManager自定义接口的跨进程通信,Stub类和Proxy都实现了ITestManager接口。Stub自己继承了Binder类来实现Binder机制,Proxy则以引用的方式实现Binder机制。
client和服务端的通信主要通过onTransact方法实现,client和服务端的数据传递通过Parcel类完毕。这里须要注意一个问题:就是在client写入数据的顺序与在服务端读取数据的顺序必须是一致的。
尽管在上面的实现中,我们定义了一个Service类TestService,但这里要说明的是Service甚至adil都不是必须的,使用者全然能够自定义。