我的Android进阶之旅------>Android服务的生命周期回调方法
来源:互联网 发布:大数据可视化工具三维 编辑:程序博客网 时间:2024/06/08 00:29
先引用一段官网上的文字
==================================================================================================
Service Lifecycle
There are two reasons that a service can be run by the system. If someone calls Context.startService()
then the system will retrieve the service (creating it and calling its onCreate()
method if needed) and then call its onStartCommand(Intent, int, int)
method with the arguments supplied by the client. The service will at this point continue running until Context.stopService()
or stopSelf()
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 stopSelf(int)
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():START_STICKY
is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY
or START_REDELIVER_INTENT
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 Context.bindService()
to obtain a persistent connection to a service. This likewise creates the service if it is not already running (callingonCreate()
while doing so), but does not call onStartCommand(). The client will receive the IBinder
object that the service returns from its onBind(Intent)
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 written in aidl.
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 Context.BIND_AUTO_CREATE
flag. Once neither of these situations hold, the service's onDestroy()
method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy().
==================================================================================================
- 当采用Context.startService()方法启动服务,与之有关的生命周期方法
- 当采用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()
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
- 我的Android进阶之旅------>Android服务的生命周期回调方法
- 我的Android进阶之旅------>Android Service学习之AIDL, Parcelable和远程服务
- 我的Android进阶之旅------>Android Service学习之AIDL, Parcelable和远程服务实例
- 我的Android进阶之旅------>Android Service学习之AIDL, Parcelable和远程服务
- 我的Android进阶之旅------>Android Service学习之AIDL, Parcelable和远程服务实例
- Android中服务的生命周期回调方法
- 我的Android进阶之旅------>android:drawableLeft的用法
- Android 服务的生命周期
- Android服务的生命周期
- Android服务的生命周期
- 我的Android进阶之旅------> Android在TextView中显示图片方法
- Android开发入门之服务的生命周期
- 我的Android进阶之旅------>Android ListView优化详解
- 我的Android进阶之旅------>Android电话窃听实例
- 我的Android进阶之旅------>Android开发错误汇总
- 我的Android进阶之旅------>Android权限参考大全
- 我的Android进阶之旅------>Android拍照小例子
- 我的Android进阶之旅------>Android KeyCode列表
- 基于linux-2.6.38.8内核的wifi驱动分析
- 已知中序遍历和后序遍历,求前序遍历
- 编码学习记录:输入包含N个自然数(N<10000),每个数不大于15000(鸽巢原理)
- 三星GT S7562 PIN 解锁方法
- POJ 2774 Long Long Message(后缀数组求最长公共子串,4级)
- 我的Android进阶之旅------>Android服务的生命周期回调方法
- Linux下Liberats SDIO wifi驱动分析
- android adapter 深刻分析
- 求n个数中最大值和最小值
- oracle导出
- FZU 1683 纪念SlingShot (构造矩阵)
- 题目1507:不用加减乘除做加法
- 为什么TCP是个可靠的协议?
- uva 846 Steps