Android ApiDemos示例解析(39):App->Service->Local Service Binding
本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities 内部类实现的。 调用的Service为LocalService。
LocalService既可以做为“Started” Service,也可以做为”Bound” Service。
一个“Bound” Service 可以通过Client/Service模式提供Service。它运行应用程序组件(比如Activity)与之绑定,然后接受请求并返回响应或者提供进程间通信机制,它的生命周期通常与调用它的组件(如Activity)相同。 而对于LocalService即作为“Started” Service又作为“Bound”Service,如果LocalService已作为“Started” Service启动,中即使所有Client都断开与它的绑定,LocalService也不会停止。
如果一个Service需要作为“Bound”Service运行其它组件与之绑定,就必须实现onBind方法。这个方法返回一个IBound对象给Client。Client可以通过这个IBind对象来调用Service的方法。
Client可以通过bindService来实现与“Bound”Service的绑定,Service 与 Client 的绑定是异步实现的,因此Client 需要通过 ServiceConnection 接口来监视与Service之间的连接。 Client 调用bindService 时,bindService 立即返回,之后当Android系统为Client 和Service之间建立起链接后调用 ServiceConnection 的 onServiceConnection 来通知Client 与Service之间的链接建立成功,并给Client返回 Service 提供的IBind对象。
LocalService 只提供给同一个Application的组件使用,不提供进程间通信接口,因此只需要派生一个Binder的子类如果直接的函数调用接口。
[java]
// Class for clients to access. Because we know
// this service always runs in the same process as
//its clients, we don't need to deal with IPC.
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
// Class for clients to access. Because we know
// this service always runs in the same process as
//its clients, we don't need to deal with IPC.
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
LocalBinder只提供了一个方法getService ,返回LocalService 对象自身。
LocalService的 onBind方法定义:
[java]
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
onBind的返回值为mBinder 为 LocalBinder类对象。它定义了一个方法getService。
再来看看Client类 LocalServiceActivities.Binding 的实现:
[java]
private LocalService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((LocalService.LocalBinder)service).getService();
// Tell the user about this for our demo.
Toast.makeText(Binding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
Toast.makeText(Binding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
private LocalService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((LocalService.LocalBinder)service).getService();
// Tell the user about this for our demo.
Toast.makeText(Binding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
Toast.makeText(Binding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
mConnection定义了ServiceConnection接口,onServiceConnected ,onServiceDisconnected分别在Service与Client 之间建立链接和断开链接时调用。其中IBinder service 就是 Service 的onBind的返回对象。 这里将其通过类型转换为LocalService也就是mBoundService。
Client 通过调用bindService建立与Service之间的绑定:
[java]
bindService(new Intent(Binding.this,
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
bindService(new Intent(Binding.this,
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
如果需要断开与Service之间的绑定,则调用unbindService.
[java] www.zzzyk.com
unbindService(mConnection);
unbindService(mConnection);[java] view plaincopyprint?
<p>如果你熟悉Socket编程, Client 绑定”Bound”Service 的方法和使用Socket通信非常类似。</p><p><a href=/2012/0630/20120630034121506.png"><img class="aligncenter size-full wp-image-1254" title="20110514001" alt="" src=/2012/0630/20120630034121506.png" width="339" height="501" /></a></p>
<p>如果你熟悉Socket编程, Client 绑定”Bound”Service 的方法和使用Socket通信非常类似。</p><p><a href=/2012/0630/20120630034121506.png"><img class="aligncenter size-full wp-image-1254" title="20110514001" alt="" src=/2012/0630/20120630034121506.png" width="339" height="501" /></a></p>
作者:mapdigit
补充:移动开发 , Android ,