混合使用startService,bindService,以及tartForegroud方法使用总结

本文详细探讨了Android中Service的生命周期,特别是startService、bindService以及startForeground方法的混合使用。阐述了Service在不同启动方式下的生命周期状态,强调了在解除绑定或停止服务时的注意事项。还提到了前台服务的概念,指出其在提高服务优先级和防止系统回收方面的优势。最后,讨论了在何时选择startService、bindService以及两者结合使用的场景,并提及了防止Service被系统回收的策略。
摘要由CSDN通过智能技术生成

一.上次的两个问题:

1.     BindService为什么不调用onServiceDisConnection()

ServiceConnection中的onServiceDisconnected()方法在正常情况下是不被调用的,它的调用时机是当Service服务被异外销毁时,例如内存的资源不足时这个方法才被自动调用。

Android系统在同service的连接意外丢失时调用这个.比如当service崩溃了或被强杀了.当客户端解除绑定时,这个方法不会被调用.

broadcastReceiver只能通过startService启动Service ,因为广播本身生命周期很短,bind的话没有意义 

2.   bindService(Intent,ServiceConnection对象,常量)

第一个参数:Intent指示对应的Service对象

第二个参数:实现了 ServiceConnection 接口的对象

第三个参数:Flags

在进行服务绑定时,其标志位可以为BIND_AUTO_CREATEBIND_DEBUG_UNBINDBIND_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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值