先引用一段官网上的文字
==================================================================================================
Service Lifecycle
There are two reasons that a service can be run by the system. If someone callsContext.startService()
then
the system will retrieve the service (creating it and calling itsonCreate()
method
if needed) and then call itsonStartCommand(Intent,
int, int)
method with the arguments supplied by the client. The service will at this point continue running untilContext.stopService()
orstopSelf()
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 theirstopSelf(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, whileSTART_NOT_STICKY
orSTART_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 useContext.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 theIBinder
object
that the service returns from itsonBind(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 beenwritten 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 startedorthere are one or more connections to it with theContext.BIND_AUTO_CREATE
flag.
Once neither of these situations hold, the service'sonDestroy()
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()
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
分享到:
相关推荐
- 各个生命周期回调函数的作用。 - 如何在适当的时间执行特定任务。 - 内存管理与资源释放。 - **第19章:处理屏幕旋转(Handling Rotation)** - **主要内容**:解释屏幕旋转时如何保存状态和恢复界面布局。 - ...
学习Android生命周期 理解Android应用的生命周期是至关重要的。`Activity`类有多个回调方法,如`onCreate`、`onStart`、`onResume`、`onPause`、`onStop`和`onDestroy`,每个对应着应用的不同状态。理解这些方法...
Activity和Service都有各自的生命周期,开发者需要理解并正确处理生命周期回调,确保应用性能和稳定性。 8. **异步操作** 使用AsyncTask、Handler、Thread、IntentService或者现代的Coroutines来处理耗时任务,...
【Android开发教程】是专为想要深入学习...总之,通过本教程,你将系统地学习到Android开发的基础和进阶知识,为你的Android开发之旅打下坚实的基础。不断学习和实践,你将能够创造出独具特色且功能丰富的Android应用。
源码中会有各种Activity或Fragment生命周期回调方法的实现,如onCreate()、onStart()、onResume()、onPause()、onStop()、onDestroy()等,这有助于理解何时进行数据保存、UI更新和其他操作。 此外,你可能还会发现...
在安卓开发领域,14天学会安卓开发是一套广受欢迎的学习资源,包含了详细的文档和配套的源码,旨在帮助初学者快速掌握安卓应用开发的基本...通过系统的阅读和实践,你将能够逐步构建自己的安卓应用,开启安卓开发之旅。