我的Android进阶之旅------>Android服务的生命周期回调方法
先引用一段官网上的文字
==================================================================================================
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()方法启动服务,与之有关的生命周期方法
 
方法在服务被创建是调用,该方法只会被调用一次,无论调用多少次startService()
方法只有采用Context.startService()方法启动服务时才会回调该方法。该方法在服务开始运行时被调用。多次调用startService()方法尽管不会多次创建服务,但是onStart()方法会被多次调用。
方法在服务被终止时调用。
- 当采用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进阶之旅------>Android利用Sensor(传感器)实现水平仪功能的小例
		
这里介绍的水平仪,指的是比较传统的气泡水平仪,在一个透明圆盘内充满液体,液体中留有一个气泡,当一端翘起时,该气泡就会浮向翘起的一端. 利用方向传感器返回的第一个参数,实现了一个指南针小应用. 我 ...
 - 我的Android进阶之旅------>Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法
		
我的Android进阶之旅-->Android颜色值(RGB)所支持的四种常见形式 透明度百分比和十六进制对应关系表格 透明度 十六进制 100% FF 99% FC 98% FA 97% F7 ...
 - 我的Android进阶之旅------>Android中查看应用签名信息
		
一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...
 - 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计
		
要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...
 - 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现
		
我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...
 - 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色
		
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
 - 我的Android进阶之旅------> Android在TextView中显示图片方法
		
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...
 - 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)
		
在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...
 
随机推荐
- pyqt sender()学习
			
#!/usr/bin/python # -*- coding: utf-8 -*- # sender.py import sys from PyQt4 import QtGui, QtCore cla ...
 - UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图
			
/* 设置圆角,通过layer中的cornerRadius和masksToBounds即可. 自适应图片宽高比例.通过UIViewContentModeScaleAsp ...
 - BOOST::Signals2
			
/* Andy is going to hold a concert while the time is not decided. Eric is a fans of Andy who doesn't ...
 - ci 笔记
			
一.CI的HelloWorld! 注意:CI禁止直接通过文件目录来访问控制器. ./application/controllers/hello.php 1 <?php 2 //放止用户直接通过路 ...
 - jquery中的 ajax 以及map遍历
			
1.语法 $.ajax{ type:'get',//类型 有get post url:'',//路径 data:{name:$('#ma').val(),nameq:$('#maq').val()}, ...
 - 【js】判断设备类型,访问相应的网站
			
引入 function uaredirect(f) { try { if (document.getElementById("bdmark") != null) { return ...
 - (转) mac 下的eclipse 配置 python 2.7
			
原地址: http://marsfreewill.blogspot.it/2012/08/mac-ospythonpydeveclipse.html 在MAC OS上配置Python开发环境(Py ...
 - Lenovo Y430P安装Linux无线网卡
			
新买了一台Lenovo Y430P的笔记本,笔记本自带的无线网卡型号是BCM43142.安装了CentOS6.5的操作系统后,按照网上搜索到的网址http://zh-cn.broadcom.com/s ...
 - java程序的10个调试技巧
			
参看下面链接:http://www.kuqin.com/java/20120906/330130.html
 - Android方法的传递值及其改变
			
MainActivity如下: package cn.testchangevar; import android.os.Bundle; import android.view.View; import ...