通过
intent i=new Intent();
intent.setcomponent(new
ComponentName(
"包名","包名类名"));
第一步,
需要修改service1项目中aidl,增加一个方法。
1
2
3
4
5
6
7
8
|
<code actionscript=
""
class
=
"hljs"
>
package
com.example.service1.aidl;
interface
IMyService {
void
basicType();
void
setName(String name);
}</code>
|
setName用于存储name的方法。
然后clear项目
第二步,
此时,我们service类中的onBind方法需要实现新接口。
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
|
<code
class
=
"hljs"
java=
""
>
package
com.example.service1;
import
android.app.Service;
import
android.content.Intent;
import
android.os.IBinder;
import
android.os.RemoteException;
import
com.example.service1.aidl.IMyService;
public
class
MyService
extends
Service {
private
String serviceName = 默认名字;
private
boolean
running;
@Override
public
IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return
new
IMyService.Stub() {
@Override
public
void
basicType()
throws
RemoteException {
// TODO Auto-generated method stub
}
@Override
public
void
setName(String name)
throws
RemoteException {
serviceName = name;
}
};
}
@Override
public
void
onCreate() {
running =
true
;
new
Thread() {
public
void
run() {
while
(running) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(serviceName);
}
};
}.start();
super
.onCreate();
}
@Override
public
void
onDestroy() {
super
.onDestroy();
running =
false
;
}
}
</code>
|
代码27到29行,接收到name并且放入全局变量中,提供给onCreate方法输出。
第三步,
将service1项目中aidl拷贝到service2项目中,并且包名要一致,
第四步,
修改service2应用activity布局,增加一个text域,和一个按钮。用于将text中的信息提交到service1项目的service中。
第五步,
修改service2项目中activity,增加与service1的通信,
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
|
<code
class
=
"hljs"
java=
""
>
package
com.example.service2;
import
com.example.service1.aidl.IMyService;
import
android.app.Activity;
import
android.content.ComponentName;
import
android.content.Context;
import
android.content.Intent;
import
android.content.ServiceConnection;
import
android.os.Bundle;
import
android.os.IBinder;
import
android.os.RemoteException;
import
android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.TextView;
public
class
MainActivity
extends
Activity
implements
OnClickListener,
ServiceConnection {
Intent serviceIntent;
private
IMyService imyService =
null
;
TextView t;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent =
new
Intent();
serviceIntent.setComponent(
new
ComponentName(com.example.service1,
com.example.service1.MyService));
findViewById(R.id.button1).setOnClickListener(
this
);
findViewById(R.id.button2).setOnClickListener(
this
);
findViewById(R.id.button3).setOnClickListener(
this
);
findViewById(R.id.button4).setOnClickListener(
this
);
findViewById(R.id.button5).setOnClickListener(
this
);
t = (TextView) findViewById(R.id.textView1);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.button1:
startService(serviceIntent);
break
;
case
R.id.button2:
stopService(serviceIntent);
break
;
case
R.id.button3:
bindService(serviceIntent,
this
, Context.BIND_AUTO_CREATE);
break
;
case
R.id.button4:
unbindService(
this
);
imyService =
null
;
break
;
case
R.id.button5:
if
(imyService !=
null
) {
try
{
imyService.setName(t.getText().toString());
}
catch
(RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public
void
onServiceConnected(ComponentName name, IBinder service) {
imyService = IMyService.Stub.asInterface(service);
System.out.println(onServiceConnected);
System.out.println(service);
}
@Override
public
void
onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
</code>
|
代码72行,传输数据
代码84行用法imyService = IMyService.Stub.asInterface(service);
运行结果,如图,