一.上次的两个问题:
1. 在BindService为什么不调用onServiceDisConnection()
类ServiceConnection中的onServiceDisconnected()方法在正常情况下是不被调用的,它的调用时机是当Service服务被异外销毁时,例如内存的资源不足时这个方法才被自动调用。
Android系统在同service的连接意外丢失时调用这个.比如当service崩溃了或被强杀了.当客户端解除绑定时,这个方法不会被调用.
broadcastReceiver只能通过startService启动Service ,因为广播本身生命周期很短,bind的话没有意义
2. bindService(Intent,ServiceConnection对象,常量)
第一个参数:Intent指示对应的Service对象
第二个参数:实现了 ServiceConnection 接口的对象
第三个参数:Flags
在进行服务绑定时,其标志位可以为BIND_AUTO_CREATE、BIND_DEBUG_UNBIND和BIND_NOT_FOREGROUND等。其中BIND_AUTO_CREATE表示当收到绑定请求时,如果服务尚未创建,则即刻创建,在系统内存不足,需要先销毁优先级组件来释放内存,且只有驻留该服务的进程成为被销毁对象时,服务才可被销毁;BIND_DEBUG_UNBIND通常用于调试场景中判断绑定的服务是否正确,但其会引起内存泄漏,因此非调试目的不建议使用;BIND_NOT_FOREGROUND表示系统将阻止驻留该服务的进程具有前台优先级,仅在后台运行,该标志位在Froyo中引入。
常量有:
Context. BIND_AUTO_CREATE: 这样就会在service不存在时创建一个
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 toan
* explicit call to {@link #startService}. Even without that, though,
* this still provides you with access tothe service object while the
* service iscreated.
Context. BIND_DEBUG_UNBIND
Flag for {@link #bindService}: include debugging help for mismatched
* calls to unbind. When this flag is set, the callstackof the following
* {@link #unbindService} call is retained, tobe printed if a later
* incorrect unbind call is made. Note that doing this requires retaining
* information about the binding that wasmade for the lifetime of the app,
* resulting ina leak -- this shouldonly be used for debugging.
Context. BIND_NOT_FOREGROUND
Flag for {@link #bindService}: don't allow this binding to raise
* the target service's process to theforeground scheduling priority.
* It will still be raised to at least thesame memory priority
* as the client (so that its process willnot be killable in any
* situation where the client is not killable),but for CPU scheduling
* purposes it may be left in thebackground. This only has an impact
* in the situation where the bindingclient is a foreground process
* and thetarget service is in a background process.
Context. BIND_ABOVE_CLIENT
Flag for {@link #bindService}: indicates that the client application
* binding to this service considers theservice to be more important than
* the app itself. When set, the platform will try to have theout of
* memory kill the app before itkills the service it is bound to, though
* this is notguaranteed to be the case.
/**
* Flag for {@link #bindService}: allow the processhosting the bound
* service to go through its normal memorymanagement. It will be
* treated more like a running service,allowing the system to
* (temporarily) expunge the process if lowon memory or for some other
* whim it may have, and being moreaggressive about making it a candidate
* to be killed (and restarted) if runningfor a long time.
*/
publicstaticfinalintBIND_ALLOW_OOM_MANAGEMENT = 0x0010;
/**
* Flag for {@link #bindService}: don't impact thescheduling or
* memory management priority of the targetservice's hosting process.
* Allows the service's process to bemanaged on the background LRU list
* just like a regular application processin the background.
*/
publicstaticfinalintBIND_WAIVE_PRIORITY = 0x0020;
/**
* Flag for {@link #bindService}: this service is veryimportant to
* the client, so should be brought to theforeground process level
* when the client is. Normally a process can only be raised to the
* visibility level by a client, even ifthat client is in the foreground.
*/
publicstaticfinalintBIND_IMPORTANT= 0x0040;
/**
* Flag for {@link #bindService}: If binding from anactivity, allow the
* target service's process importance tobe raised based on whether the
* activity is visible to the user,regardless whether another flag is
* used to reduce the amount that theclient process's overall importance
* is used to impact it.
*/
publicstaticfinalintBIND_ADJUST_WITH_ACTIVITY = 0x0080;
/**
* Flag for {@link #bindService}: Don't consider thebound service to be
* visible, even if the caller is visible.
* @hide
*/
public