【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnecti

本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,做为LocalServiceActivities 内部类实现的。 调用的Service为LocalService。编程

LocalService既能够作为“Started” Service,也能够作为”Bound” Service。app

一个“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的方法。ide

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的子类若是直接的函数调用接口。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 对象自身。spa

LocalService的 onBind方法定义:code

    @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。component

再来看看Client类 LocalServiceActivities.Binding 的实现:对象

        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之间的绑定:

        void doBindService() {
            // Establish a connection with the service.  We use an explicit
            // class name because we want a specific service implementation that
            // we know will be running in our own process (and thus won't be
            // supporting component replacement by other applications).
            bindService(new Intent(Binding.this, 
                    LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
            mIsBound = true;
        }

 

若是须要断开与Service之间的绑定,则调用unbindService.

        void doUnbindService() {
            if (mIsBound) {
                // Detach our existing connection.
                unbindService(mConnection);
                mIsBound = false;
            }
        }

 

若是你熟悉Socket编程, Client 绑定”Bound”Service 的方法和使用Socket通讯很是相似。

相关文章
相关标签/搜索