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的生命周期

时间: 2024-08-05 00:57:30

Andorid总结 - Bound Services的相关文章

API翻译 --- Bound Services

目录: 1.The Basics 最基本的 2.Declaring a service in the manifest 在服务清单中配置 3.Creating a Started Service Extending the IntentService class 创建一个服务扩展类 4.Extending the Service class 继承Serrvice类 5.Starting a service 开始服务 6.Stopping a service 结束服务 7.Creating a B

Android官方文档之Bound Services

绑定式Service在CS结构中扮演着Server的角色.绑定式Service允许其他组件(如Activity)绑定该Service.发送请求.接收响应.甚至IPC通信( interprocess communication).绑定式Service通常服务于其他应用程序的组件.且没有明确的后台的概念(does not run in the background indefinitely). 本文将介绍bound Service的相关内容,包括其创建.与其他应用组件如何绑定 等.有关Service

2.App Components-Services/Bound Services

1. Bound Services A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound

Android总结 - Service

Service是一个长时间操作的后台服务,也可以做IPC操作. Service有两种启动模式:Started和Bound.所谓"started"就是通过调用startService()而Bound就是通过调用bindService(). Service的生命周期 通过Service的生命周期可以得到Server的几个重要的回调函数: onStartCommand() 当其他组件,如 activity 请求服务启动时,系统会调用这个方法.一旦这个方法执行,服务就开始并且无限期的执行.如果

Develop系列-API Guides-应用组件-Services-Bound Services

Bound Services CS架构,其中C是调用组件,S是Bound Services: C通过bindService来绑定,这个方法立即返回,没有返回值,C需要实现ServiceConnection里面的onServiceConnected和onServiceDisconnected接口. 多个C绑定同一个S时,S只调用一次onBind返回IBinder,后续来绑定S的C,直接得到同一个IBinder,onBind不再重复执行. 创建Bound Services 扩展Binder类:只在应

【Android】16.2 Started Services

分类:C#.Android.VS2015: 创建日期:2016-03-01 一.简介 Started Service是指被同一个应用程序的某个对象显式启动,或者在设备引导时就已经启动了(配置了服务的情况). 二.Started Services的生命周期 前面我们说过,Service只是一种被分离出来的组件(例如从某个Activity中分离出来),可被单独启动启动和停止.因此不论是Started Services还是Bound Services,这些Services都有它自己独立的生命周期. 下

Android开发文档翻译之-Services

Service是一种能长期在后台运行同时不需要与用户进行交互的应用组件.其他组件可以开启service,开启后service可以自行运行及时用户已经切换到其他的应用.此外,组件可以与service进行绑定来进行交互,及时是跨进程的交互(Android的IPC机制).网络操作.播放音乐.执行文件IO操作或是与内容提供者进行交互,这些操作都可以通过service在后台进行. Service的两种形式: Started 通过调用startService(),你可以启动一个service.一旦被启动,s

Android开发-API指南-Bound 类型的服务

Bound Services 英文原文:http://developer.android.com/guide/components/bound-services.html 采集(更新)日期:2014-12-24 原博客:http://blog.sina.com.cn/s/blog_48d4913001010696.html 在本文中 简介 创建一个 Bound 服务 扩展 Binder 类 使用 Messenger 绑定一个服务 管理 Bound 服务的生命周期 关键类 Service Serv

Android02.常用布局及基本UI控件

一.Android学习API指南:[了解] 1. 应用的组成部分   App Components 1.1. 应用的基本原理    App Fundamentals 1.2. Activity      Activities 1.2.1. 片段    Fragments 1.2.2. 加载器     Loaders 1.2.3. 任务和返回堆    Tasks and Back Stack 1.3. Service服务   Services 1.3.1. 绑定服务     Bound Servi