01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class MyService extends Service {
public class MyBinder extends Binder {
/**
* 获取Service的运行时间
* @return
*/
public long getServiceRunTime() {
return System.currentTimeMillis() - startTime;
}
}
private long startTime;
/**
* MyBinder是Binder的子类, 而Binder实现了IBinder接口。
*/
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
@Override
public void onCreate() {
super .onCreate();
startTime = System.currentTimeMillis();
}
}
|
01
02
03
04
05
06
07
08
09
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
|
public class MainActivity extends Activity {
private MyBinder binder = null ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建一个指向MyService的intent
Intent intent = new Intent( "cn.xing.action.my_service" );
this .bindService(intent, new MyServiceConnection(),
Service.BIND_AUTO_CREATE);
Button button = (Button) this .findViewById(R.id.button);
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (binder != null ) {
Toast.makeText(getApplicationContext(), "MyService已经运行了" + binder.getServiceRunTime()
+ "毫秒" , Toast.LENGTH_LONG).show();
}
}
});
}
/**
* 实现ServiceConnection接口
*
* @author xing
*
*/
private final class MyServiceConnection implements ServiceConnection {
/**
* 和MyService绑定时系统回调这个方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// MyService中的onBinder()方法的返回值实际上是一个MyBinder对象, 因此可以使用强制转换.
binder = (MyBinder) service;
}
/**
* 解除和MyService的绑定时系统回调这个方法
*/
@Override
public void onServiceDisconnected(ComponentName name) {
// 解除和MyService的绑定后, 将binder设置为null.
binder = null ;
}
}
}
|
01
02
03
04
|
package cn.xing.remoteservice;
interface IRemoteService{
int getServiceRunTime();
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class RemoteService extends Service {
private long startTime;
/**
* IRemoteService.Stub类实现了IBinder和IRemoteService接口
* 因此Stub的子类对象可以作为onBinder()方法的返回值.
* @author xing
*
*/
public class MyBinder extends IRemoteService.Stub {
@Override
public long getServiceRunTime() throws RemoteException {
return System.currentTimeMillis() - startTime;
}
};
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
@Override
public void onCreate() {
super .onCreate();
startTime = System.currentTimeMillis();
}
}
|
01
02
03
04
05
06
07
08
09
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
|
public class MainActivity extends Activity {
private IRemoteService iRemoteService = null ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建一个指向RemoteService的intent
Intent intent = new Intent( "cn.xing.action.remote_service" );
this .bindService(intent, new MyServiceConnection(),
Service.BIND_AUTO_CREATE);
Button button = (Button) this .findViewById(R.id.button);
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (iRemoteService != null ) {
try {
Toast.makeText(getApplicationContext(), "MyService已经运行了" + iRemoteService.getServiceRunTime()
+ "毫秒" , Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
}
/**
* 实现ServiceConnection接口
*
* @author xing
*
*/
private final class MyServiceConnection implements ServiceConnection {
/**
* 和RemoteService绑定时系统回调这个方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 此处不能使用强制转换, 应该调用Stub类的静态方法获得IRemoteService接口的实例对象
iRemoteService = IRemoteService.Stub.asInterface(service);
}
/**
* 解除和RemoteService的绑定时系统回调这个方法
*/
@Override
public void onServiceDisconnected(ComponentName name) {
// 解除和RemoteService的绑定后, 将iRemoteService设置为null.
iRemoteService = null ;
}
}
}
|