android之aidl的简单使用
aidl这里就不加累述它的概念定义等等,免得长篇大幅。下面介绍的是我第一次使用aidl成功与service通信的一个例子:
1.在项目包下新建一个IInfo.aidl,并在其中添加你要调用的方法,格式和java中接口一样。
01
package com.android.server;
02
03
inte易做图ce IInfo {
04
boolean start();
05
void stop();
06
void locate(int x, int y);
07
void move(int dx, int dy);
08
void getLocation(<span style="background-color:#ff9900;">inout</span> int[] p);//参数为数组的话,可以加上inout,不然会报错
09
void setTimeout(int t);
10
int getTimeout();
11
void setBitmap(<span style="background-color:#ff9900;">inout</span> byte[] bmp, int width, int height);}
正确写好之后,eclipse的adt会自动在gen目录下生成一个IInfo.java文件
2.新建一个MyService.java类,继承IInfo.stubruxia,如下:
01
package com.android.server;
02
03
public class CursorService extends ICursorInfo.Stub{
04
final boolean hasService;
05
public CursorService() {
06
hasService = initializeJNI();
07
}
08
09
public synchronized boolean start() {
10
if (hasService)
11
return start0();
12
return false;
13
}
14
15
public synchronized void stop() {
16
if (hasService)
17
stop0();
18
}
19
20
public synchronized void locate(int x, int y) {
21
if (hasService)
22
locate0(x, y);
23
}
24
25
public synchronized void move(int dx, int dy) {
26
if (hasService)
27
move0(dx, dy);
28
}
29
30
public synchronized void getLocation(int[] p) {
31
if (p == null)
32
throw new NullPointerException("p is null");
33
if (p.length < 2)
34
throw new IllegalArgumentException("p.len must >= 2");
35
if (hasService)
36
getPosition0(p);
37
}
38
39
public synchronized void setTimeout(int t) {
40
if (hasService)
41
setTimeout0(t);
42
}
43
44
public synchronized int getTimeout() {
45
if (hasService)
46
return getTimeout0();
47
return -1;
48
}
49
50
public void setBitmap(byte[] bmp, int width, int height) {
51
if(bmp == null)
52
throw new NullPointerException("bmp is null");
53
if(width < 0 || height < 0)
54
throw new IllegalArgumentException("width < 0 || height < 0");
55
if(width * height > bmp.length)
56
throw new IndexOutOfBoundsException("bmp less than width*height");
57
setBitmap0(bmp,width,height);
58
}
在其中实现你aidl中的方法
3. 新建一个Manager类,在其中构造一个内部服务连接类,实现ServiceConnection接口:
01
public class Manager {
02
private static final String TAG = "Manager";
03
04
private IInfo iCurSer;
05
06
private Manager(){
07
}
08
09
public Manager(Context ctx){
10
this.context = ctx;
11
new Manager();
12
}
13
14
/**这里就可以与service正常通信,调用service中的方法**/
15
16
public void startService(){
17
Intent intent=new Intent("com.android.server.CursorService");
18
context.bindService(intent,new CursorServiceConnection(),
19
Service.BIND_AUTO_CREATE);
20
}
21
22
/**
23
* 实现ServiceConnection接口
24
* */
25
26
public final class CursorServiceConnection implements ServiceConnection{
27
// 和CursorService绑定时系统回调这个方法
28
@Override
29
public void onServiceConnected(ComponentName name, IBinder service) {
30
// 此处不能使用强制转换, 应该调用Stub类的静态方法获得CursorService接口的实例对象
31
iCurSer=ICursorInfo.Stub.asInte易做图ce(service);
32
}
33
34
//解除和CursorService的绑定时系统回调这个方法
35
@Override
36
public void onServiceDisconnected(ComponentName name) {
37
&nb
补充:移动开发 , Android ,