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 anIBinder
interface. 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 theIntent
that 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 ...
随机推荐
- office outlook 無法開啟 outlook 視窗
例如[無法啟動Microsoft Office Outlook.無法開啟Outlook 視窗.] 1.啟動 Outlook 安全模式outlook.exe /safe2.清除並重新產生目前設定檔的功能 ...
- Office EXCEL 的绝对引用和相对引用如何理解
比如C1 = A1+B1,则我把C1的单元格往下拖拉的时候,C2会自动等于A2+B2,C3会自动等于A3+B3,而如果让G1 = $E$1+$F$1,则把G1单元格往下拖拉的时候,G2G3单元格都不会 ...
- Android手机中UID、PID作用及区别
PID 指进程ID. PID是进程的身份标识,程序一旦运行,就会给应用分配一个独一无二的PID(ps:一个应用可能包含多个进程,每个进程有唯一的一个PID) 进程终止后PID会被系统收回,再次打开应用 ...
- 【转载】How browsers work--Behind the scenes of modern web browsers (前端必读)
浏览器可以被认为是使用最广泛的软件,本文将介绍浏览器的工 作原理,我们将看到,从你在地址栏输入google.com到你看到google主页过程中都发生了什么. 将讨论的浏览器 今天,有五种主流浏览器- ...
- C++实现KMP模式匹配算法
#include<iostream> #include<string> #include<vector> using namespace std; void Nex ...
- MIUI应用权限设置
不管你认为我写的好坏都能够在以下评论告诉我,你的支持是我继续写下去的动力,谢谢. 随着miui越来越封闭,小米对非自由渠道的应用限制越来越苛刻.我们公司的产品一半以上的用户都是来自小米,并且像我们这种 ...
- codeforces 394E Lightbulb for Minister 简单几何
题目链接:点我点我 题意:给定n个点. 以下n行给出这n个点坐标. 给定m个点,以下m行给出这m个点坐标. 这m个点是一个凸包,顺时针给出的. 问:在凸包上随意找一个点(x, y) 使得这个点距离n个 ...
- TF-IDF与余弦类似性的应用(一):自己主动提取关键词
作者: 阮一峰 日期: 2013年3月15日 原文链接:http://www.ruanyifeng.com/blog/2013/03/tf-idf.html 这个标题看上去好像非常复杂,事实上我要谈的 ...
- CSDN公开课:SCRUM敏捷开发(2015-8-19 免费)
当前最火的敏捷可能就是SCRUM了.但敏捷无法落地.对人要求太高.老板对敏捷动机不良等问题怎样解决呢?我将在CSDN的公开课上为大家分享"SCRUM敏捷开发".各位朋友有杀错没放过 ...
- oracle 12c 13姨
搞了一下oracle 12c.有些体会还是先记下来. 12c搞搞新意思,弄了个CDB(容器数据库,可不是商务中心CBD哟)和PDB(可插拔数据库).PDB插在CDB里. 简单而言,CDB就是一个数据库 ...