先引用一段官网上的文字
==================================================================================================
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
==================================================================================================
分享到:
相关推荐
我的Android进阶之旅------>Android疯狂连连看游戏的实现 可以参考博客:http://blog.csdn.net/ouyang_peng/article/details/14115627
Android高手进阶教程之----Android 通用获取Ip的方法(判断手机是否联网的方法)!!!.doc Android高手进阶教程之----Android 在一个应用中如何启动另外一个已安装的应用!!!.doc Android高手进阶教程之----Android 中...
我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端 (地址:http://blog.csdn.net/ouyang_peng/article/details/47004617) 我的Android进阶之旅------>Android实现用...
深入理解jvm虚拟机+Android进阶之光-附件资源
我的Android进阶之旅------>Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能 http://blog.csdn.net/ouyang_peng/article/details/50813419 1、实现歌词同步滚动的功能,即歌曲播放到哪句...
为了满足您的要求,我将基于Android高级进阶这一主题,提供一些该领域的通用知识点,希望能够帮助到您。 Android高级进阶通常包括如下几个方面的深入学习: 1. Android系统架构:深入理解Android系统底层架构,...
Android 高手进阶教程(二)之----Android Launcher 抽屉类 SlidingDrawer 的 使用 最近在研究 Lanucher ,看了源码,发现了 SlidingDrawer 这个类,也就是 所谓的"抽屉"类。它的用法很简单,要包括 handle ,和 content...
Android知识体系图&面试&进阶(Version-1.0.1).xmind
《Android平台开发之旅(第2版)》涵盖Android 3/4的新特性,立足实际的开发案例,介绍了Android平台开发的基础概念、实用技术和应用模式。主要内容包括应用程序框架、高级界面、数据库应用、网络通信与Web开发、无线...
在本文中,作者分享了关于Android开发中常用命令的集锦以及如何使用Android中的...这些内容对于想要进阶为Android高手的开发者尤为宝贵,可以帮助他们熟练运用Android SDK提供的工具和类库,更加高效地完成开发任务。
android-support-v4-v7-v13-v14-v17(官方最新完整版),官方最新版的,压缩包内包含android-support-v4、android-support-v7-appcompat,android-support-v7-cardview,android-support-v7-gridlayout,android-support-...
内容包含:android-support-v4_1.6.0_26_20120316.jar android-support-v4_1.6.0_26_20120623.jar android-support-v4_1.6.0_26_20120730.jar android-support-v4_1.6.0_26_20121109.jar android-support-v4_1.6.0_26...
背景: android活动的生命周期包括以下回调: onCreate() , onStart() , onResume() , onPause() , onStop() , onRestart()和onDestroy() 。 这些回调为开发人员提供了指示应用程序行为的区域。onCreate() 此...
标题中的“android-studio-ide-193.6626763-windows.exe”指的是Android Studio 4.0的特定版本,适用于Windows操作系统。虽然这个版本不是最新的,但它仍然包含了丰富的功能和优化,对于开发者来说,理解其特性和...
Android开发进阶 从小工到专家-2016.2-P378.pdf Android开发进阶 从小工到专家-2016.2-P378.pdf Android开发进阶 从小工到专家-2016.2-P378.pdf