Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互。

1、Service的继承关系:

java.lang.Object → android.content.Context → android.content.ContextWrapper → android.app.Service

2、Sevice定义:

Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件。其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行。另外,一个组件能够绑定到一个service与之交互(IPC机制),例如,一个service可能会处理网络操作,播放音乐,操作文件I/O或者与内容提供者(content provider)交互,所有这些活动都是在后台进行。

3、Service有两种状态," 启动的 " 和 " 绑定 ":

[1]通过startService()启动的服务处于" 启动的 "状态,一旦启动,service就在后台运行,即使启动它的应用组件已经被销毁了。通常started状态的service执行的任务并且不返回任何结果给启动者。比如当下载或上传一个文件,当这项操作完成时,service应该停止它本身。

[2]" 绑定 "状态:通过调用bindService()来启动,一个绑定的service提供一个允许组件与service交互的接口,可以发送请求、获取返回结果,还可以通过夸进程通信来交互(IPC)。绑定的service只有当应用组件绑定后才能运行,多个组件可以绑定一个service,当调用unbind()方法时,这个service就会被销毁了。

注意:默认情况下,service与activity一样都存在与当前进程的主线程中,所以,一些阻塞UI的操作,比如耗时操作不能放在service里进行,比如另外开启一个线程来处理诸如网络请求的耗时操作。如果在service里进行一些耗CPU和耗时操作,可能会引发ANR警告,这时应用会弹出是强制关闭还是等待的对话框。所以,对service的理解就是和activity平级的,只不过是看不见的,在后台运行的一个组件,这也是为什么和activity同被说为Android的基本组件。启动后的Service具有较高的优先级,一般情况下,系统会保证Service的正常运行,只有当前台的Activity正常运行的资源被Service占用的情况下,系统才会停止Service;当系统重新获得资源后,会根据程序的设置来决定是否重新启动原来的Service。

4、Service的生命周期:

        

 注意:bind service的不同之处在于当绑定的组件销毁后,对应的service也就被kill了。

service生命周期也涉及一些回调方法,这些方法都不用调用父类方法,具体如下:

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
}
}

5、Service的一个子类:IntentService

IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,因此,这里提供一个思路,如果有耗时的操作与其在Service里面开启新线程还不如使用IntentService来处理耗时操作。而在一般的继承Service里面如果要进行耗时操作就必须另开线程,但是使用IntentService就可以直接在里面进行耗时操作,因为默认实现了一个worker thread。

实现实例:

public class defaultIntentService extends IntentService {

  //必须有构造函数, 并且必须带有worker thread的name作为参数调用super IntentService(String)
public defaultIntentService() {
super("defaultIntentService");
} //通过从默认worker thread调用此带有intent的方法启动service
//当方法返回IntentService会适时停止service
@Override
protected void onHandleIntent(Intent intent) {
// 通常我们会做一些比如下载文件等的工作.
// 比如实例中,睡眠五秒.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
}
}

6、停止Service

[1]如果service是非绑定的,最终当任务完成时,为了节省系统资源,一定要停止service,可以通过stopSelf()来停止,也可以在其他组件中通过stopService()来停止;

[2]绑定的service可以通过onUnBind()来停止service。

    

最新文章

  1. sublime text添加snippet
  2. nagios二次开发(四)---nagios监控原理和nagios架构简介
  3. uboot.lds (一)
  4. JQ基础练习---图片划过变暗
  5. poj 1753 Flip Game 枚举(bfs+状态压缩)
  6. 在Linux下安装C/C++开发工具包的最佳方式
  7. PTA 5-12 How Long Does It Take (25分)
  8. 同一台电脑上安装两个tomcat服务器
  9. Qt Quick App的两种启动模式
  10. Java多线程实现生产者消费者延伸问题
  11. Delphi中类的运行期TypeInfo信息结构说明
  12. hdu_5742_It&#39;s All In The Mind
  13. h5可预览 图片ajax上传 (补更),后台数据获取方法---php
  14. UWP:可滚动的PivotHeader
  15. Linux OpenGL 实践篇-1 OpenGL环境搭建
  16. MATLAB cftool工具数据拟合结果好坏判断
  17. 【原创】Linux基础之上传下载
  18. 关于TSql
  19. 《机器学习实战(基于scikit-learn和TensorFlow)》中英文资源+源码 下载
  20. Dethe is my Finaunce金融

热门文章

  1. BlueMind 3.0.17 发布,消息和协作平台
  2. ant design Modal遮罩层颜色加深 解决方案
  3. Swift教程之枚举语法
  4. error MSB8031: Building an MFC project for a non-Unicode character set is deprecated
  5. MVC扩展ActionInvoker,自定义ActionInvoker,根据请求数据返回不同视图
  6. Windows 7 下玩游戏不能全屏
  7. window消息机制二
  8. 关于JAVA多线程并发synchronized的测试与合理使用
  9. Asp.Net中自以为是的Encode
  10. MapReduce模式MapReduce patterns