Android Service之bindService

本文主要是记录关于bindService的用法的翻译和记录。
先看英文介绍

一、关于方法介绍

boolean bindService (Intent service,
ServiceConnection conn,
int flags)

Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when it is 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.

This function will throw SecurityException if you do not have permission to bind to the given service.

Note: this method can not be called from a BroadcastReceiver component. A pattern you can use to communicate from a BroadcastReceiver to a Service is to call startService(Intent) with the arguments containing the command to be sent, with the service calling its 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 a BroadcastReceiver that has been registered with registerReceiver(BroadcastReceiver, IntentFilter), since the lifetime of this BroadcastReceiver is tied to another object (the one that registered it).

翻译:
连接到应用程序服务,如果需要创建它。这定义了应用程序和服务之间的依赖关系。给定的conn将在创建服务对象时接收该服务对象,并告知其是否死机并重新启动。只要调用上下文存在,系统就会认为该服务是必需的。例如,如果此上下文是已停止的活动,则在恢复活动之前,不需要服务继续运行。

如果您没有绑定到给定服务的权限,此函数将抛出SecurityException。

注意:此方法不能从BroadcastReceiver组件调用。可以用来从BroadcastReceiver到服务的通信模式是使用包含要发送的命令的参数调用startService(Intent),在完成执行该命令时,服务调用其stopSelf(int)方法。请参阅API演示应用程序/服务/服务启动参数控制器的插图。但是,从使用registerReceiver(BroadcastReceiver,IntentFilter)注册的BroadcastReceiver中使用此方法是可以的,因为此BroadcastReceiver的生命周期与另一个对象(注册它的对象)绑定。

二、入参介绍

入参主要有3个参数,分别传入Intent,ServiceConnection对象和一个int变量,这里就不在翻译了。

这里写图片描述

三、第3个参数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.

BIND_AUTO_CREATE

Added in API level 1
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.

Constant Value: 1 (0x00000001)

翻译:用于bindService的标志(Intent,ServiceConnection,int):只要存在绑定,就自动创建服务。注意,虽然这将创建服务,它的onStartCommand(Intent,int,int)方法将仍然只被调用由于显式调用startService(Intent)。即使没有这个,虽然,这仍然提供您在服务创建时访问服务对象。

请注意,在ICE_CREAM_SANDWICH之前,不提供此标志也将影响系统考虑目标服务进程的重要性。当设置时,它提出的唯一方式是通过绑定从服务,在这种情况下,它只会是重要的,当该活动是在前台。现在要实现此行为,必须显式提供新标志BIND_ADJUST_WITH_ACTIVITY。为了兼容性,未指定BIND_AUTO_CREATE的旧应用程序将自动为它们设置标志BIND_WAIVE_PRIORITY和BIND_ADJUST_WITH_ACTIVITY,以便实现相同的结果。

常数值:1(0x00000001)

BIND_DEBUG_UNBIND

Added in API level 1
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.

Constant Value: 2 (0x00000002)

翻译:bindService标志(Intent,ServiceConnection,int):包含对unbind调用不匹配的调试帮助。 设置此标志时,将保留以下unbindService(ServiceConnection)调用的调用堆栈,如果稍后发生了不正确的解除绑定调用,则会打印该堆栈。 请注意,这样做需要保留有关应用程序生命周期的绑定的信息,从而导致泄漏 - 这应该只用于调试。

常数值:2(0x00000002)

BIND_NOT_FOREGROUND

Added in API level 8
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.

Constant Value: 4 (0x00000004)

翻译:bindService的标志(Intent,ServiceConnection,int):不允许此绑定将目标服务的进程提升到前台调度优先级。 它仍然会被提升到至少与客户端相同的内存优先级(以使其进程不会在客户端不可杀死的任何情况下杀死),但是对于CPU调度目的,它可能留在后台。 这仅在绑定客户端是前台进程并且目标服务在后台进程中的情况下具有影响。

常数值:4(0x00000004)

BIND_ABOVE_CLIENT

Added in API level 14
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.

Constant Value: 8 (0x00000008)

翻译:
在API级别14中添加
int BIND_ABOVE_CLIENT
bindService的标志(Intent,ServiceConnection,int):表示绑定到此服务的客户端应用程序认为该服务比应用程序本身更重要。 当设置时,平台将尝试使内存杀手杀死应用程序,然后杀死它绑定到的服务,尽管这不能保证是这种情况。

BIND_ALLOW_OOM_MANAGEMENT

Added in API level 14
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.

Constant Value: 16 (0x00000010)

翻译:
BIND_ALLOW_OOM_MANAGEMENT

在API级别14中添加
int BIND_ALLOW_OOM_MANAGEMENT
bindService的标志(Intent,ServiceConnection,int):允许承载绑定服务的进程通过其正常的内存管理。 它将更像是一个正在运行的服务,允许系统(暂时)清除过程,如果内存低或一些其他的想法可能有,并更积极的使它成为候选人被杀死(并重新启动)if 运行很长时间。

常数值:16(0x00000010)

BIND_WAIVE_PRIORITY

Added in API level 14
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.

Constant Value: 32 (0x00000020)

翻译:
BIND_WAIVE_PRIORITY

在API级别14中添加
int BIND_WAIVE_PRIORITY
bindService的标志(Intent,ServiceConnection,int):不影响目标服务的主机进程的调度或内存管理优先级。 允许在后台LRU列表上管理服务的进程,就像在后台的常规应用程序进程一样。

常数值:32(0x00000020)

猜你喜欢

转载自blog.csdn.net/u012489412/article/details/54140554