博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的Android进阶之旅------>Android服务的生命周期回调方法
阅读量:6546 次
发布时间:2019-06-24

本文共 3394 字,大约阅读时间需要 11 分钟。

            先引用一段官网上的文字

==================================================================================================

Service Lifecycle

There are two reasons that a service can be run by the system. If someone calls  then the system will retrieve the service (creating it and calling its  method if needed) and then call its  method with the arguments supplied by the client. The service will at this point continue running until  or  is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their  method to ensure the service is not stopped until started intents have been processed.

For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): is used for services that are explicitly started and stopped as needed, while  or  are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.

Clients can also use  to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling while doing so), but does not call onStartCommand(). The client will receive the  object that the service returns from its  method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder). Usually the IBinder returned is for a complex interface that has been .

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the  flag. Once neither of these situations hold, the service's  method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy().

==================================================================================================

  • 当采用Context.startService()方法启动服务,与之有关的生命周期方法
onCreate()-->onStart()-->onDestory()
onCreate()
方法在服务被创建是调用,该方法只会被调用一次,无论调用多少次startService()
或者bindService()方法,服务也只被创建一次。
onStart()
方法只有采用Context.startService()方法启动服务时才会回调该方法。该方法在服务开始运行时被调用。多次调用startService()方法尽管不会多次创建服务,但是onStart()方法会被多次调用。
onDestory()
方法在服务被终止时调用。
  • 当采用Context.bindService()方法启动服务,与之有关的生命周期方法

onCreate()-->onBind()-->onUnbind()-->onDestory()

onBind()方法只有采用Context.bindService()启动服务时才会回调该方法。该方法在调用者和服务绑定时被调用,当调用者和服务已经绑定,多次调用Context.bindService()方法并不会导致该方法多次被调用。

onUnbind()方法只有采用Context.bindService()启动服务时才会回调该方法。该方法在调用者和服务解除绑定时被调用。

  • 如果先采用Context.startService()方法启动服务,然后调用Context.bindService()方法绑定到服务,再调用Context.unbindService()方法解除绑定,最后调用Context.bindService()方法再次绑定到服务,触发的生命周期方法如下:

onCreate()-->onStart()-->onBind()-->onUnbind()[重载后的方法需返回true]-->onRebind()

==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址

==================================================================================================

转载于:https://www.cnblogs.com/ouyangpeng/p/8538262.html

你可能感兴趣的文章
join命令实现文件内容拼接
查看>>
-bash:wget command not found的解决方法
查看>>
yara规则
查看>>
我的个人简历
查看>>
我的友情链接
查看>>
图片的copy,从一个目录复制到另一个目录
查看>>
我的友情链接
查看>>
KVM组件bug报告方法
查看>>
linux内核实现中的小工具
查看>>
xtrabackup备份、恢复
查看>>
jboss端口配置
查看>>
格式化输出数字字符串
查看>>
mongodb慢查询定位
查看>>
第四十八天:三方协议的内容
查看>>
shell基础之管道符和变量
查看>>
python_备份mysql数据库
查看>>
[UNIX环境高级编程]apue.h头文件的配置
查看>>
linux nagios hist_dat_root__32 Error with user uid=517(nagios) gid=517(nagios) groups=517(nagios)
查看>>
【Python】从0开始写爬虫——扒一下狗东
查看>>
sql server 查询某个表被哪些存储过程调用
查看>>