Service官方教程(10)onUnbind、onRebind与BIND_AUTO_CREATE等绑定标记

1.绑定服务的生命期

官方原文如下:

Managing the Lifecycle of a Bound Service

  When a service is unbound from all clients, the Android system destroys it (unless it was also started with onStartCommand()). As such, you don't have to manage the lifecycle of your service if it's purely a bound service—the Android system manages it for you based on whether it is bound to any clients.

  However, if you choose to implement the onStartCommand() callback method, then you must explicitly stop the service, because the service is now considered to be started. In this case, the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients.

  Additionally, if your service is started and accepts binding, then when the system calls your onUnbind() method, you can optionally return true if you would like to receive a call to onRebind() the next time a client binds to the service. onRebind() returns void, but the client still receives the IBinder in its onServiceConnected() callback. Below, figure 1 illustrates the logic for this kind of lifecycle.

  Figure 1. The lifecycle for a service that is started and also allows binding.

  For more information about the lifecycle of a started service, see the Services document.

总结如下:

  • 同一个服务组件,无论有多少个客户端绑定到它,只onCreate,onBind一次,并且返回同一个binder
  • 在最后一个客户端unbindService之后,onUnbind会被调用。
  • onUnbind返回true ,在服务组件停止之前,如果有新的客户端再次绑定到这个它,onRebind会被调用,并且返回的还是原来那个binder,且不会调用onBind

 

2.绑定标记

源码:

 1     /** @hide */
 2     @IntDef(flag = true, prefix = { "BIND_" }, value = {
 3             BIND_AUTO_CREATE,
 4             BIND_DEBUG_UNBIND,
 5             BIND_NOT_FOREGROUND,
 6             BIND_ABOVE_CLIENT,
 7             BIND_ALLOW_OOM_MANAGEMENT,
 8             BIND_WAIVE_PRIORITY,
 9             BIND_IMPORTANT,
10             BIND_ADJUST_WITH_ACTIVITY,
11             BIND_NOT_PERCEPTIBLE,
12             BIND_INCLUDE_CAPABILITIES
13     })
14     @Retention(RetentionPolicy.SOURCE)
15     public @interface BindServiceFlags {}

对比表:

参数
 值 描述
0
0
不指定绑定参数
BIND_AUTO_CREATE
0x0001
/**
* 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>Note that prior to {@link android.os.Build.VERSION_CODES#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
* {@link #BIND_ADJUST_WITH_ACTIVITY}. For compatibility, old applications
* that don't specify {@link #BIND_AUTO_CREATE} will automatically have
* the flags {@link #BIND_WAIVE_PRIORITY} and
* {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve
* the same result.
*/
 
 
BIND_DEBUG_UNBIND
 
0x0002
 
/**
* 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.
*/
 
BIND_NOT_FOREGROUND
 
0x0004
/**
* 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 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.
*/
 
BIND_ABOVE_CLIENT
 
0x0008
 
/**
* Flag for {@link #bindService}: 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.
*/
 
 
BIND_ALLOW_OOM_MANAGEMENT
 
0x0010
 
/**
* Flag for {@link #bindService}: 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.
*/
 
BIND_WAIVE_PRIORITY
 
0x0020
/**
* Flag for {@link #bindService}: 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.
*/
 
 
BIND_IMPORTANT
 
0x0040
 
/**
* Flag for {@link #bindService}: this service is very important to
* the client, so should be brought to the foreground process level
* when the client is. Normally a process can only be raised to the
* visibility level by a client, even if that client is in the foreground.
*/
 
BIND_ADJUST_WITH_ACTIVITY
 
0x0080
 
/**
* Flag for {@link #bindService}: If binding from an activity, allow the
* target service's process importance to be raised based on whether the
* activity is visible to the user, regardless whether another flag is
* used to reduce the amount that the client process's overall importance
* is used to impact it.
*/
 
BIND_NOT_PERCEPTIBLE
 
0x00000100
 
/**
* Flag for {@link #bindService}: If binding from an app that is visible
* or user-perceptible,lower the target service's importance to below the
* perceptible level.This allows the system to (temporarily) expunge the
* bound process from memory to make room for more important user-perceptible
* processes.
*/
 
BIND_INCLUDE_CAPABILITIES
 
0x000001000
 
/**
* Flag for {@link #bindService}: If binding from an app that has specific
* capabilities due to its foreground state such as an activity or foreground
* service, then this flag will allow the bound app to get the same capabilities,
* as long as it has the required permissions as well.
*/
posted @ 2016-09-23 17:09  f9q  阅读(1212)  评论(0编辑  收藏  举报