Context类bindService(Intent service, ServiceConnection conn, int flags)参数

  1. public abstract boolean bindService(Intent service, ServiceConnection conn, int flags); 

参数 flags的值有如下情况:
  1. public static final int BIND_AUTO_CREATE       表明只要绑定存在,就自动建立 Service;同时也告知Android系统,这个Service的重要程度与调用者相同, 除非考虑终止调用者,否则不要关闭这个Service
  2. public static final int BIND_DEBUG_UNBIND
  3. public static final int BIND_NOT_FOREGROUND
  4. public static final int BIND_ABOVE_CLIENT
  5. public static final int BIND_WAIVE_PRIORITY

bindService()是Context的一个方法,它是抽象的。函数原型的代码如下:(android 2.3.3)

 
  1.  * 

    This function will throw {@link SecurityException} if you do not 

  2.  * have permission to bind to the given service. 
  3.  * 
  4.  * Note: this method can not be called from an 
  5.  * {@link BroadcastReceiver} component.  A pattern you can use to 
  6.  * communicate from an BroadcastReceiver to a Service is to call 
  7.  * {@link #startService} with the arguments containing the command to be 
  8.  * sent, with the service calling its 
  9.  * {@link android.app.Service#stopSelf(int)} method when done executing 
  10.  * that command.  See the API demo App/Service/Service Start Arguments 
  11.  * Controller for an illustration of this.  It is okay, however, to use 
  12.  * this method from an BroadcastReceiver that has been registered with 
  13.  * {@link #registerReceiver}, since the lifetime of this BroadcastReceiver 
  14.  * is tied to another object (the one that registered it).

     
  15.  * 
  16.  * @param service Identifies the service to connect to.  The Intent may 
  17.  *      specify either an explicit component name, or a logical 
  18.  *      description (action, category, etc) to match an 
  19.  *      {@link IntentFilter} published by a service. 
  20.  * @param conn Receives information as the service is started and stopped. 
  21.  * @param flags Operation options for the binding.  May be 0, 
  22.  *          {@link #BIND_AUTO_CREATE}, {@link #BIND_DEBUG_UNBIND}, or 
  23.  *          {@link #BIND_NOT_FOREGROUND}. 
  24.  * @return If you have successfully bound to the service, true is returned; 
  25.  *         false is returned if the connection is not made so you will not 
  26.  *         receive the service object. 
  27.  * 
  28.  * @throws SecurityException 
  29.  * 
  30.  * @see #unbindService 
  31.  * @see #startService 
  32.  * @see #BIND_AUTO_CREATE 
  33.  * @see #BIND_DEBUG_UNBIND 
  34.  * @see #BIND_NOT_FOREGROUND 
  35.  */  
  36. public abstract boolean bindService(Intent service, ServiceConnection conn,  int flags);  

而最终实现bindService()方法的是ContextImpl这个类,代码如下:

[java]  view plain copy
  1. @Override  
  2.     public boolean bindService(Intent service, ServiceConnection conn,  
  3.             int flags) {  
  4.         IServiceConnection sd;  
  5.         if (mPackageInfo != null) {  
  6.             sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),  
  7.                     mMainThread.getHandler(), flags);  
  8.         } else {  
  9.             throw new RuntimeException("Not supported in system context");  
  10.         }  
  11.         try {  
  12.             int res = ActivityManagerNative.getDefault().bindService(  
  13.                 mMainThread.getApplicationThread(), getActivityToken(),  
  14.                 service, service.resolveTypeIfNeeded(getContentResolver()),  
  15.                 sd, flags);  
  16.             if (res < 0) {  
  17.                 throw new SecurityException(  
  18.                         "Not allowed to bind to service " + service);  
  19.             }  
  20.             return res != 0;  
  21.         } catch (RemoteException e) {  
  22.             return false;  
  23.         }  
  24.     }  
这里暂且不讨论是如何实现bindService()的。

[java]  view plain copy
  1. 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_CREATEBIND_DEBUG_UNBINDBIND_NOT_FOREGROUNDBIND_ABOVE_CLIENT,BIND_ALLOW_OOM_MANAGEMENT, or BIND_WAIVE_PRIORITY.

service和conn比较好理解。flags意思是绑定时的选项,可以有很多选项,一一来看。

public static final int BIND_AUTO_CREATE

Added in  API level 1

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 specifyBIND_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)


public static final int BIND_DEBUG_UNBIND

Added in  API level 1

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)


public static final int BIND_NOT_FOREGROUND

Added in  API level 8

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)


public static final int BIND_ABOVE_CLIENT

Added in  API level 14

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)


public static final int BIND_ALLOW_OOM_MANAGEMENT

Added in  API level 14

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)


public static final int BIND_WAIVE_PRIORITY

Added in  API level 14

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)


常量源码android 2.3.3

[java]  view plain copy
  1. /** 
  2.  * Flag for {@link #bindService}: automatically create the service as long 
  3.  * as the binding exists.  Note that while this will create the service, 
  4.  * its {@link android.app.Service#onStartCommand} 
  5.  * method will still only be called due to an 
  6.  * explicit call to {@link #startService}.  Even without that, though, 
  7.  * this still provides you with access to the service object while the 
  8.  * service is created. 
  9.  * 
  10.  * 

    Specifying this flag also tells the system to treat the service 

  11.  * as being as important as your own process -- that is, when deciding 
  12.  * which process should be killed to free memory, the service will only 
  13.  * be considered a candidate as long as the processes of any such bindings 
  14.  * is also a candidate to be killed.  This is to avoid situations where 
  15.  * the service is being continually created and killed due to low memory. 
  16.  */  
  17. public static final int BIND_AUTO_CREATE = 0x0001;  
  18.   
  19. /** 
  20.  * Flag for {@link #bindService}: include debugging help for mismatched 
  21.  * calls to unbind.  When this flag is set, the callstack of the following 
  22.  * {@link #unbindService} call is retained, to be printed if a later 
  23.  * incorrect unbind call is made.  Note that doing this requires retaining 
  24.  * information about the binding that was made for the lifetime of the app, 
  25.  * resulting in a leak -- this should only be used for debugging. 
  26.  */  
  27. public static final int BIND_DEBUG_UNBIND = 0x0002;  
  28.   
  29. /** 
  30.  * Flag for {@link #bindService}: don't allow this binding to raise 
  31.  * the target service's process to the foreground scheduling priority. 
  32.  * It will still be raised to the at least the same memory priority 
  33.  * as the client (so that its process will not be killable in any 
  34.  * situation where the client is not killable), but for CPU scheduling 
  35.  * purposes it may be left in the background.  This only has an impact 
  36.  * in the situation where the binding client is a foreground process 
  37.  * and the target service is in a background process. 
  38.  */  
  39. public static final int BIND_NOT_FOREGROUND = 0x0004;

你可能感兴趣的:(Android)