博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 绑定类型服务---绑定服务
阅读量:4684 次
发布时间:2019-06-09

本文共 2000 字,大约阅读时间需要 6 分钟。

应用程序组件(客户端)通过调用bindService()方法能够绑定服务,然后Android系统会调用服务的onBind()回调方法,这个方法会返回一个跟服务端交互的IBinder对象。

这个绑定是异步的,bindService()方法立即返回,并且不给客户端返回IBinder对象。要接收IBinder对象,客户端必须创建一个ServiceConnection类的实例,并且把这个实例传递给bindService()方法。ServiceConnection对象包含了一个系统调用的传递IBinder对象的回调方法。

注意:只有Activity、Service、和内容提供器(content provider)能够绑定服务---对于广播接收器不能绑定服务。

因此,要在客户端绑定一个服务,必须做以下事情:

1.  实现ServiceConnection抽象类

你的实现必须重写以下两个回调方法:

onServiceConnected()

    系统会调用这个方法来发送由服务的onBind()方法返回的IBinder对象。

onServiceDisconnected()

     Android系统会在连接的服务突然丢失的时候调用这个方法,如服务崩溃时或被杀死时。在客户端解除服务绑定时不会调用这个方法。

2.  调用bindService()方法来传递ServiceConnection类的实现;

3.  当系统调用你的onServiceConnected()回调方法时,你就可以开始使用接口中定义的方法来调用服务了;

4.  调用unbindService()方法断开与服务的连接。

当客户端销毁时,它将于服务端解绑,但是当你完成与服务端的交互或Activity暂停时,你应该主动的与服务端解绑,以便服务能够在不使用的时候关掉。(绑定和解绑的时机会在稍后进行更多的讨论)。

例如,以下代码片段演示了把客户端连接到由继承Binder类创建服务,它所做的所有事情就是把服务端返回的IBinder对象转换成LocalService类,并且请求LocalService实例:

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;
    }
};

用这个ServiceConnection类,客户端能够把它传递给bindService()方法来实现与服务端的绑定,如:

Intent intent = new Intent(this, LocalService.class);

bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

1. bindService方法的第一个参数是一个Intent对象,它明确的命名要绑定的服务(虽然Intent能够隐含的指定);

2. 第二个参数是ServiceConnection对象;

3. 第三个参数是指明绑定选项的标识。通常它应该使用BIND_AUTO_CREATE,以便在服务不存在的时候创建这个服务。其他可能的值是BIND_DEBUG_UNBIND和BIND_NOT_FOREGROUND,或是什么都没有的“0”。

转载于:https://www.cnblogs.com/phonegap/archive/2012/02/28/2535861.html

你可能感兴趣的文章
解决Cannot change version of project facet Dynamic web module to 2.5
查看>>
linux下压缩文件乱码
查看>>
Java NIO Buffer(四)
查看>>
四. Java继承和多态8.Java final关键字:阻止继承和多态
查看>>
C Primer Plus note3
查看>>
电脑待机、休眠、睡眠的区别
查看>>
打算做一款给方便学生生活的APP,虽然已经有口袋小安了,但是并没有我想要的功能。。。...
查看>>
Oracle SQL*Loader commit point tips
查看>>
基础物理
查看>>
linux下解除端口占用
查看>>
POJ 1404 I-Keyboard (DP)
查看>>
POJ 4044 Score Sequence
查看>>
某种密码(搜索专练)
查看>>
【BZOJ5305】【HAOI2018】—苹果树(组合数学)
查看>>
【BZOJ3821】【UOJ#46】【清华集训2014】—玄学(线段树分治)
查看>>
【leetcode 简单】 第八十三题 反转字符串中的元音字母
查看>>
【leetcode 简单】 第一百零八题 找到所有数组中消失的数字
查看>>
引用同一解决方案的类库工程不成功
查看>>
[转]单例模式中为什么用枚举更好
查看>>
selenium 获取断言信息
查看>>