【移动开发】Context类bindService()参数
bindService()是Context的一个方法,它是抽象的。函数原型的代码如下:(android 2.3.3)
/**
* Connect to an application service, creating it if needed. This defines
* a dependency between your application and the service. The given
* <var>conn</var> will receive the service object when its created and be
* told if it dies and restarts. The service will be considered required
* by the system only for as long as the calling context exists. For
* example, if this Context is an Activity that is stopped, the service will
* not be required to continue running until the Activity is resumed.
*
* <p>This function will throw {@link SecurityException} if you do not
* have permission to bind to the given service.
*
* <p class="note">Note: this method <em>can not be called from an
* {@link BroadcastReceiver} component</em>. A pattern you can use to
* communicate from an BroadcastReceiver to a Service is to call
* {@link #startService} with the arguments containing the command to be
* sent, with the service calling its
* {@link android.app.Service#stopSelf(int)} method when done executing
* that command. See the API demo App/Service/Service Start Arguments
* Controller for an illustration of this. It is okay, however, to use
* this method from an BroadcastReceiver that has been registered with
* {@link #registerReceiver}, since the lifetime of this BroadcastReceiver
* is tied to another object (the one that registered it).</p>
*
* @param service Identifies the service to connect to. The Intent may
* specify either an explicit component name, or a logical
* description (action, category, etc) to match an
* {@link IntentFilter} published by a service.
* @param conn Receives information as the service is started and stopped.
* @param flags Operation options for the binding. May be 0,
* {@link #BIND_AUTO_CREATE}, {@link #BIND_DEBUG_UNBIND}, or
* {@link #BIND_NOT_FOREGROUND}.
* @return If you have successfully bound to the service, true is returned;
* false is returned if the connection is not made so you will not
* receive the service object.
*
* @throws SecurityException
*
* @see #unbindService
* @see #startService
* @see #BIND_AUTO_CREATE
* @see #BIND_DEBUG_UNBIND
* @see #BIND_NOT_FOREGROUND
*/
public abstract boolean bindService(Intent service, ServiceConnection conn,
int flags);
而最终实现bindService()方法的是ContextImpl这个类,代码如下:
@Override
public boolean bindService(Intent service, ServiceConnection conn,
int flags) {
IServiceConnection sd;
if (mPackageInfo != null) {
sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
mMainThread.getHandler(), flags);
} else {
throw new RuntimeException("Not supported in system context");
}
try {
int res = ActivityManagerNative.getDefault().bindService(
mMainThread.getApplicationThread(), getActivityToken(),
service, service.resolveTypeIfNeeded(getContentResolver()),
sd, flags);
if (res < 0) {
throw new SecurityException(
"Not allowed to bind to service " + service);
}
return res != 0;
} catch (RemoteException e) {
return false;
}
}
这里暂且不讨论是如何实现bindService()的。
public abstract boolean bindService(Intent service, ServiceConnection conn,
int flags);
从函数原型可知,bindService()有3个参数,官方文档解释如下:
service | Identifies the service to connect to. The Intent may specify either an explicit component name, or a logical description (action, category, etc) to match an IntentFilter published by a service. |
---|---|
conn | Receives information as the service is started and stopped. This must be a valid ServiceConnection object; it must not be null. |
flags | Operation options for the binding. May be 0, BIND_AUTO_CREATE , BIND_DEBUG_UNBIND , BIND_NOT_FOREGROUND , BIND_ABOVE_CLIENT , BIND_ALLOW_OOM_MANAGEMENT , or BIND_WAIVE_PRIORITY . |
service和conn比较好理解。flags意思是绑定时的选项,可以有很多选项,一一来看。
public static final int BIND_AUTO_CREATE
Flag for bindService(Intent, ServiceConnection, int)
: automatically create the service as long as the binding exists. Note that while this will create the service, its onStartCommand(Intent, int, int)
method will still only be called due to an explicit call to startService(Intent)
. Even without that, though, this still provides you with access to the service object while the service is created.
Note that prior to ICE_CREAM_SANDWICH
, not supplying this flag would also impact how important the system consider's the target service's process to be. When set, the only way for it to be raised was by binding from a service in which case it will only be important when that activity is in the foreground. Now to achieve this behavior you must explicitly supply the new flag BIND_ADJUST_WITH_ACTIVITY
. For compatibility, old applications that don't specify BIND_AUTO_CREATE
will automatically have the flags BIND_WAIVE_PRIORITY
and BIND_ADJUST_WITH_ACTIVITY
set for them in order to achieve the same result.
public static final int BIND_DEBUG_UNBIND
Flag for bindService(Intent, ServiceConnection, int)
: include debugging help for mismatched calls to unbind. When this flag is set, the callstack of the following unbindService(ServiceConnection)
call is retained, to be printed if a later incorrect unbind call is made. Note that doing this requires retaining information about the binding that was made for the lifetime of the app, resulting in a leak -- this should only be used for debugging.
public static final int BIND_NOT_FOREGROUND
Flag for bindService(Intent, ServiceConnection, int)
: don't allow this binding to raise the target service's process to the foreground scheduling priority. It will still be raised to at least the same memory priority as the client (so that its process will not be killable in any situation where the client is not killable), but for CPU scheduling purposes it may be left in the background. This only has an impact in the situation where the binding client is a foreground process and the target service is in a background process.
public static final int BIND_ABOVE_CLIENT
Flag for bindService(Intent, ServiceConnection, int)
: indicates that the client application binding to this service considers the service to be more important than the app itself. When set, the platform will try to have the out of memory killer kill the app before it kills the service it is bound to, though this is not guaranteed to be the case.
public static final int BIND_ALLOW_OOM_MANAGEMENT
Flag for bindService(Intent, ServiceConnection, int)
: allow the process hosting the bound service to go through its normal memory management. It will be treated more like a running service, allowing the system to (temporarily) expunge the process if low on memory or for some other whim it may have, and being more aggressive about making it a candidate to be killed (and restarted) if running for a long time.
public static final int BIND_WAIVE_PRIORITY
Flag for bindService(Intent, ServiceConnection, int)
: don't impact the scheduling or memory management priority of the target service's hosting process. Allows the service's process to be managed on the background LRU list just like a regular application process in the background.
常量源码android 2.3.3
/**
* Flag for {@link #bindService}: automatically create the service as long
* as the binding exists. Note that while this will create the service,
* its {@link android.app.Service#onStartCommand}
* method will still only be called due to an
* explicit call to {@link #startService}. Even without that, though,
* this still provides you with access to the service object while the
* service is created.
*
* <p>Specifying this flag also tells the system to treat the service
* as being as important as your own process -- that is, when deciding
* which process should be killed to free memory, the service will only
* be considered a candidate as long as the processes of any such bindings
* is also a candidate to be killed. This is to avoid situations where
* the service is being continually created and killed due to low memory.
*/
public static final int BIND_AUTO_CREATE = 0x0001; /**
* Flag for {@link #bindService}: include debugging help for mismatched
* calls to unbind. When this flag is set, the callstack of the following
* {@link #unbindService} call is retained, to be printed if a later
* incorrect unbind call is made. Note that doing this requires retaining
* information about the binding that was made for the lifetime of the app,
* resulting in a leak -- this should only be used for debugging.
*/
public static final int BIND_DEBUG_UNBIND = 0x0002; /**
* Flag for {@link #bindService}: don't allow this binding to raise
* the target service's process to the foreground scheduling priority.
* It will still be raised to the at least the same memory priority
* as the client (so that its process will not be killable in any
* situation where the client is not killable), but for CPU scheduling
* purposes it may be left in the background. This only has an impact
* in the situation where the binding client is a foreground process
* and the target service is in a background process.
*/
public static final int BIND_NOT_FOREGROUND = 0x0004;
【移动开发】Context类bindService()参数的更多相关文章
- Android开发——Context类的各种细节问题
0. 前言 Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学习一下那些你所不知道的细节. ...
- Android开发中Context类的作用以及Context的详细用法
Android中Context的作用以及Context的详细用法 本文我们一起来探讨一下关于Android中Context的作用以及Context的详细用法,这对我们学习Android的资源访问有很大 ...
- 二次封装Response类 | 视图类传递参数给序列化类context
二次封装Response类 源码: class Response(SimpleTemplateResponse): """ An HttpResponse that al ...
- Android Context类
一.Context类的使用场景 ①.创建Application对象时,而且整个app公用一个Application对象 ②.创建Service对象时 ③.创建Activity对象时 二.应用程序共有几 ...
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
- Java开发工具类集合
Java开发工具类集合 01.MD5加密工具类 import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...
- c++特性:指向类成员的指针和非类型类模板参数和函数指针返回值 参数推导机制和关联型别
一.c++允许定义指向类成员的指针,包括类函数成员指针和类数据成员指针 格式如下: class A { public: void func(){printf("This is a funct ...
- 源码心德`Context`类
Context,中文直译为“上下文”,SDK中对其说明如下: Interface to global information about an application environment. Thi ...
- JAVA之等号、传类对象参数与c++的区别
在JAVA中用等号对类对象进行赋值,实际上操作的是对象的地址. eg: package MyText; class ClassA { int value; public void seta(int v ...
随机推荐
- bzoj 3672: [Noi2014]购票
Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的 ...
- 对中断的理解handle_level_irq【原创】
如下为本人原创,在解决问题的过程中的一点心得,如果有描述不准确的地方还请各位指出,非常感谢 Linux内核版本:linux-4.9.18 曾有一次调试触摸屏的时候遇到如下的问题 /startup/mo ...
- Java面试题—初级(8)
基本表结构: student(sno,sname,sage,ssex)学生表 course(cno,cname,tno) 课程表 sc(sno,cno,score) 成绩 ...
- jqGrid移动滑块时冻结首列和第二列例子
js代码如以下代码:在initAllGrid函数colModel1加入属性设置:frozen:true:然后在_initGrid('task_con_grid_div',colModel1)函数里面加 ...
- mac下IDLE无法输入中文的问题
解决方法是下载安装新版本的 Tcl/Tk 下载地址:http://www.activestate.com/activetcl/downloads 注意要下最新的8.5.18.0版本,安装好了再重启ID ...
- 《cocos2d-x游戏开发之旅》问题2016-10-7
今天按书上做,遇到问题卡住了 书P115 项目是 littlerunner
- gulp填坑记(二)——gulp多张图片自动合成雪碧图
为优化图片,减少请求会把拿到切好的图标图片,通过ps(或者其他工具)把图片合并到一张图里面,再通过css定位把对于的样式写出来引用的html里面,对于一些图片较多的项目,这个过程可能要花费我们一天的时 ...
- JS的事件模型
之前对事件模型还是比较清楚的,许多概念都清晰映射在脑海中.工作之后,一方面使用的局限性,二是习惯于用框架中的各种事件监听方式,简单即方便,久而久之,事件的一些概念开始淡出记忆中,就像我现在已经开始淡忘 ...
- Ubuntu 16.04+.Net Core+Docker+Uginx安装部署
前言 最近公司的项目打算移植到.Net Core平台,所以调研了一下.Net Core在Linux下的安装部署.本篇文章会一步步的描述从安装到配置到部署的全部过程.在文章的结构和内容里,笔者借鉴了很多 ...
- jQuery 效果 – 淡入淡出
在在jQuery中可以通过四个方法来实现元素的淡入淡出,这四个方法分别是:fadeIn().fadeOut().fadeToggle() 以及 fadeTo(),本文通过实例来为你讲解如何在jQuer ...