Service官方教程(4)两种Service的生命周期函数
Managing the Lifecycle of a Service
The lifecycle of a service is much simpler than that of an activity. However, it's even more important that you pay close attention to how your service is created and destroyed, because a service can run in the background without the user being aware.
The service lifecycle—from when it's created to when it's destroyed—can follow two different paths:
- A started service
The service is created when another component calls
startService(). The service then runs indefinitely and must stop itself by callingstopSelf(). Another component can also stop the service by callingstopService(). When the service is stopped, the system destroys it.. - A bound service
The service is created when another component (a client) calls
bindService(). The client then communicates with the service through anIBinderinterface. The client can close the connection by callingunbindService(). Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. (The service does not need to stop itself.)
These two paths are not entirely separate. That is, you can bind to a service that was already started with startService(). For example, a background music service could be started by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService(). In cases like this, stopService() or stopSelf() does not actually stop the service until all clients unbind.
started和bound类的service 生命周期函数不是完全独立的,started的service也可以被绑定,这时要先解绑才能停止。
Implementing the lifecycle callbacks 如何实现service生命周期函数
Like an activity, a service has lifecycle callback methods that you can implement to monitor changes in the service's state and perform work at the appropriate times. The following skeleton service demonstrates each of the lifecycle methods:
public class ExampleService extends Service {
int mStartMode; // indicates how to behave if the service is killed
IBinder mBinder; // interface for clients that bind
boolean mAllowRebind; // indicates whether onRebind should be used
@Override
public void onCreate() {
// The service is being created
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
}
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed
}
}
Note: Unlike the activity lifecycle callback methods, you are not required to call the superclass implementation of these callback methods.
service的生命周期函数,不是必需调用父类的,如super.xxx。

Figure 2. The service lifecycle. The diagram on the left shows the lifecycle when the service is created with startService() and the diagram on the right shows the lifecycle when the service is created with bindService().
By implementing these methods, you can monitor two nested loops of the service's lifecycle:
- The entire lifetime of a service happens between the time
onCreate()is called and the timeonDestroy()returns. Like an activity, a service does its initial setup inonCreate()and releases all remaining resources inonDestroy(). For example, a music playback service could create the thread where the music will be played inonCreate(), then stop the thread inonDestroy().The
onCreate()andonDestroy()methods are called for all services, whether they're created bystartService()orbindService(). - The active lifetime of a service begins with a call to either
onStartCommand()oronBind(). Each method is handed theIntentthat was passed to eitherstartService()orbindService(), respectively.If the service is started, the active lifetime ends the same time that the entire lifetime ends (the service is still active even after
onStartCommand()returns). If the service is bound, the active lifetime ends whenonUnbind()returns.-started的service一直存活,直到系统清理内存时,才有可能杀死它。
-bound的service活到onUnbind()函数返回。
Note: Although a started service is stopped by a call to either stopSelf() or stopService(), there is not a respective callback for the service (there's no onStop() callback). So, unless the service is bound to a client, the system destroys it when the service is stopped—onDestroy() is the only callback received.
Figure 2 illustrates the typical callback methods for a service. Although the figure separates services that are created by startService() from those created by bindService(), keep in mind that any service, no matter how it's started, can potentially allow clients to bind to it. So, a service that was initially started with onStartCommand() (by a client calling startService()) can still receive a call to onBind() (when a client calls bindService()).
For more information about creating a service that provides binding, see the Bound Services document, which includes more information about the onRebind() callback method in the section about Managing the Lifecycle of a Bound Service.
Service官方教程(4)两种Service的生命周期函数的更多相关文章
- Service的两种用法及其生命周期
先来一点基础知识: Service 是android的四大组件之一,与Activity同属于一个级别,它是运行在后台进行服务的组件(例如在后台播放的音乐,播放音乐的同时并不影响其他操作).Servic ...
- 两种Service如何一起使用
1.先是调用startservice来开启服务,并在且在后台一起运行. 2.在调用bindservice,获取中间对象. 3.unbindservice解绑服务. 4.最后在调用stopservice ...
- service的生命周期以及两种service的差异
可以看到,两种service的生命周期都相对简单,有一点不同的是,Intentservice每次调用的时候都执行onstartcommand,而boundservice一旦启动了之后,就不会每次执行o ...
- Service官方教程(6)Bound Services主要用来实现通信服务,以及3种实现通信的方案简介。
1.Bound Services A bound service is the server in a client-server interface. A bound service allows ...
- org.springframework.stereotype.Service和com.alibaba.dubbo.config.annotation.Service两种service的区别
这两个Service,都可以在service类头上使用@Service的注解,于是我就写错了,查了半天才发现.他们的区别大概是这个样子的: org.springframework.stereotype ...
- 关于android中两种service的编写简单总结
1.startservice (两种方法,继承service类或者继承intentservice 类) 继承service类,在onstartcommend重载方法中实现业务逻辑的处理,如果耗时过长最 ...
- Service官方教程(2)*IntentService与Service示例、onStartCommand()3个返回值的含义。
1.Creating a Started Service A started service is one that another component starts by calling start ...
- Service官方教程(1)Started与Bound的区别、要实现的函数、声明service
Services 简介和分类 A Service is an application component that can perform long-running operations in the ...
- Service官方教程(11)Bound Service示例之2-AIDL 定义跨进程接口并通信
Android Interface Definition Language (AIDL) 1.In this document Defining an AIDL Interface Create th ...
随机推荐
- 运行计划中cost计算方法
概念: blevel:二元高度=索引高度-1 clustering_factor:集群因子,通过索引扫面得出的要查询table的blocks数量,clustering_factor接近table的bl ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- conda安装速度慢解决办法
注意,清华已经撤掉其ananconda源, 下面的方法已经失效,中科大源好像也不行,如果有解决办法烦请评论告诉我. conda config --add channels https://mirror ...
- JavaScript语句-流程控制语句
JavaScript定义了一组语句,语句通常用于执行一定的任务.语句可以很简单,也可以很复杂. 选择结构,可以在程序中创建交叉结构来指定程序流的可能方向.JavaScript中有四种选择结构: 1.单 ...
- IDEA debug调式快捷键
F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 Step Over 相当于eclipse的f6 跳到下一步 F7 Step ...
- 如何写好react组件
react 组件方面: 总结 React 组件的三种写法 及最佳实践 [涨经验] React组件编写思路(一) 使用react-router实现单页面应用时设置页面间过渡的两种方式 [翻译]基于 Cr ...
- MySql安装与使用图文教程
2.下载完成后将其解压到你想要安装的路径下,例如我的解压到D:\MySql\mysql-5.7.12-winx64\路径下,刚解压完应该是下图这些文件夹:最好解压到根目录. 5.新建一个my.in ...
- Dom4J XML转bean
package com.baiwang.bop.utils; import com.baiwang.bop.client.BopException; import org.dom4j.Element; ...
- BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包
BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...
- 【Jsoi2010】连通数
[题目链接] 点击打开链接 [算法] 直接暴力dfs一遍,即可 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN ...