本例和下列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的子类如果直接的函数调用接口。

// 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方法定义:

    @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 的实现:

        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通信非常类似。

【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder的更多相关文章

  1. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...

  2. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  3. 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener

    前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...

  4. 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面

    我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...

  5. 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式

    这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...

  6. 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState

    Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...

  7. 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller

    Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...

  8. 【起航计划 033】2015 起航计划 Android APIDemo的魔鬼步伐 32 App->Service->Foreground Service Controller service使用,共享service,前台服务,onStartCommand

    Android系统也提供了一种称为“Service”的组件通常在后台运行.Activity 可以用来启动一个Service,Service启动后可以保持在后台一直运行,即使启动它的Activity退出 ...

  9. 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考

    01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:

随机推荐

  1. 6A - Daydreamin

    #include <iostream> #include <cstdio> using namespace std; typedef long long ll; ll dp[] ...

  2. c#Udp分包组包方法

    udp通信协议,相信大家都知道这个.由于是无连接的协议,所有udp的传输效率比tcp高.但是udp协议传输较大的数据文件得分包 最近写了个分包组包的方法,拿来和大家分享,如果有什么不妥的地方,欢迎点评 ...

  3. C++_异常8-异常、类和基础

    异常.类和继承以三种方式相互关联. 首先,可以像标准C++库所做的那样,从一个异常类派生出另一个. 其次,可以在类定义中嵌套异常类声明来组合异常. 第三,这种嵌套声明本身可以被继承,还可以作为基类. ...

  4. C++_代码重用4-多重继承

    继承使用时要注意,默认是私有派生.所以要公有派生时必须记得加关键字Public. MI(Multi Inheritance)会带来哪些问题?以及如何解决它们? 两个主要问题: 从两个不同的基类继承同名 ...

  5. LeetCode905.按奇偶排序数组

    905.按奇偶排序数组 问题描述 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例 输入:[3,1,2, ...

  6. 【AC自动机】【字符串】【字典树】AC自动机 学习笔记

    blog:www.wjyyy.top     AC自动机是一种毒瘤的方便的多模式串匹配算法.基于字典树,用到了类似KMP的思维.     AC自动机与KMP不同的是,AC自动机可以同时匹配多个模式串, ...

  7. POJ - 1222 / POJ - 3279 枚举第一行

    说好的高斯消元法呢,暴搜都能0ms 这种翻转就是枚举第一行控制变量下面行就全都确定了 代码参考挑战程序设计例题 #include<iostream> #include<algorit ...

  8. 认识HTML中表格、列表标签以及表单控件

    前端之HTML,CSS(二) HTML标签 列表标签 无序列表:闭标签,由<ul><li></li>...</ul>组合而成,效果成纵向列表.格式:&l ...

  9. vuex中filter的使用 && 快速判断一个数是否在一个数组中

    vue中filter的使用 computed: mapState({ items: state => state.items.filter(function (value, index, arr ...

  10. Ant利用第三方的task

    转自 http://blog.sina.com.cn/s/blog_3d21e545010006s9.html 一.如何使用第三方任务   Ant可以使用第三方任务,在使用第三方任务之前,需告知Ant ...