Andorid总结 - Bound Services

创建一个Bound Service

想要创建一个提供binding的service,必须提供一个IBinder给client与service进行交互。有三种方式可以定义接口:

  • 继承Binder类
    如果service只给自己的应用使用并且不会做跨进程的操作,我们可以继承Binder类并且通过onBind()返回一个它的实例。client接收到这个Binder可以直接使用它开调用service提供的方法。
    使用步骤:
    1. 创建一个Binder实例,任意实现一种:
      • 在Binder类中包含client可以访问的public方法。
      • 返回一个当前Service的实例,client可以通过这个实例调用Service的public方法。
      • 返回一个封装了service public方法的类对象,client可以调用。
    2. 通过onBind()回调函数返回Binder实例。
    3. 在client端,通过onServiceConnected()的回调函数接收Binder 对象,通过Binder对象来调用公开的方法。

例子,通过onBinder返回Service对象:

public class LocalService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    // Random number generator
    private final Random mGenerator = new Random();

    /** * Class used for the client Binder. 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 this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
    }
}
  • 使用Messenger
    如果service需要与别的进程进行交互,那么可以使用Messenger提供的接口来实现service。这个技术不需要使用AIDL就能实现IPC。
    Messenger的使用方法:
    1. service需要实现一个Handler来接收client调用时传递过来的callback。
    2. 通过Handler来创建一个 Messenger对象。
    3. 这个Messenger 对象创建一个IBinder,提供给onBind()返回给client。
    4. Clients 使用 IBinder来初始化这个Messenger (that references the service’s Handler), client可以使用这个Messenger 来给service发送Message对象。
    5. Service中实现的Handler会接收并处理接收到的Message。
      使用Messenger来实现的Service实例:
public class MessengerService extends Service {
    /** Command to the service to display a message */
    static final int MSG_SAY_HELLO = 1;

    /** * Handler of incoming messages from clients. */
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_SAY_HELLO:
                    Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    /** * Target we publish for clients to send messages to IncomingHandler. */
    final Messenger mMessenger = new Messenger(new IncomingHandler());

    /** * When binding to the service, we return an interface to our messenger * for sending messages to the service. */
    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
        return mMessenger.getBinder();
    }
}

一个调用Messenger实现的Service的客服端实例:

public class ActivityMessenger extends Activity {
    /** Messenger for communicating with the service. */
    Messenger mService = null;

    /** Flag indicating whether we have called bind on the service. */
    boolean mBound;

    /** * Class for interacting with the main interface of the service. */
    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 object we can use to
            // interact with the service. We are communicating with the
            // service using a Messenger, so here we get a client-side
            // representation of that from the raw IBinder object.
            mService = new Messenger(service);
            mBound = true;
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mService = null;
            mBound = false;
        }
    };

    public void sayHello(View v) {
        if (!mBound) return;
        // Create and send a message to the service, using a supported 'what' value
        Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
        try {
            mService.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to the service
        bindService(new Intent(this, MessengerService.class), mConnection,
            Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }
}
  • Using AIDL

Binding to a Service

想要绑定Service,Client需要实现:


  1. 实现ServiceConnection接口,必须实现两个方法。

    • onServiceConnected(),系统会调用这个函数用于返回IBinder。
    • onServiceDisconnected(),当连接异常断掉之后,系统会调用这个函数。unbind的时候不会调用。

  2. 调用bindSercie,把ServiceConnection 作为参数传递过去。
  3. 当系统调用onServiceConnected() 之后,就可以 通过获取到的IBinder来调用service公开的方法。
  4. 调用unbindService()来断开连接。

实现ServiceConnection的实例:

LocalService mService;
private ServiceConnection mConnection = new ServiceConnection() {
    // Called when the connection with the service is established
    public void onServiceConnected(ComponentName className, IBinder service) {
        // Because we have bound to an explicit
        // service that is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    // Called when the connection with the service disconnects unexpectedly
    public void onServiceDisconnected(ComponentName className) {
        Log.e(TAG, "onServiceDisconnected");
        mBound = false;
    }
};

调用bindService():

Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

bindService的第三个参数指示binding时的选项。通常应该设置为BIND_AUTO_CREATE 。还可以为BIND_DEBUG_UNBIND 和BIND_NOT_FOREGROUND, 或者设为0代表什么都不选.

Additional notes

binding to a service的时候有几个注意事项:

  • 应该总是捕捉DeadObjectException 异常, 这种异常会在链接被破坏掉的时候抛出来。 这是远程方法会抛出的唯一的一种异常。
  • 跨进程处理的时候,Object的引用会被计数。
  • 需要在client的生命周期中成对的使用binding和unbinging。例如:
    • 如果只需要在Activity可见的时候使用service,应该在onStart的时候bind servcie,在onStop的时候unbind srvice。
    • 如果想Activity在后台的时候也与service交互,那么在onCreate的时候bind service,在onDestroy的时候unbind service。

管理Bound Service的生命周期

Andorid总结 - Bound Services_第1张图片

你可能感兴趣的:(android)