Android中Service与IntentService的使用比较
不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentService的解释,发现了它相对于Service来说有很多更加方便之处,今天在这里稍微来总结下我的心得。
首先IntentService是继承自Service的,那我们先看看Service的官方介绍,这里列出两点比较重要的地方:
1.A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
2.A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
1.Service不是一个单独的进程 ,它和应用程序在同一个进程中
2.Service不是一个线程,所以我们应该避免在Service里面进行耗时的操作
有一点需要强调,如果有耗时操作在Service里,就必须开启一个单独的线程来处理,这点一定要铭记在心。
IntentService相对于Service来说,有几个非常有用的优点,首先我们看看官方文档的说明:
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
翻译过来是:使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,这里就给我们提供了一个思路,如果有耗时的操作与其在Service里面开启新线程还不如使用IntentService来处理耗时操作。
由于服务进程的优先级高于后台进程, 因此如果activity需要执行耗时操作, 最好还是启动一个service来完成. 当然, 在activity中启动子线程完成耗时操作也可以,但是这样做的缺点在于,一旦activity不再可见,activity所在的进程成为后台进程, 而内存不足时后台进程随时都有可能被系统杀死(但是启动service完成耗时操作会带来数据交互的问题, 比如耗时操作需要实时更新UI控件的状态的话,service就不是一个好的选择)。 基于同样的考虑, 在BroadcastReceiver中也不应该执行耗时操作, 而应该启动service来完成(当然, BroadcastReceiver的生命周期过于短暂, 也决定了不能在其中执行耗时操作)。
总结:IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的Service,使用时你只需要继承IntentService和重写其中的onHandleIntent(Intent)方法接收一个Intent对象,在适当的时候会停止自己(一般在工作完成的时候)。所有的请求的处理都在一个工作线程中完成,它们会交替执行(但不会阻塞主线程的执行),一次只能执行一个请求。
这是一个基于消息的服务,每次启动该服务并不是马上处理你的工作,而是首先会创建对应的Looper,Handler并且在MessageQueue中添加的附带客户Intent的Message对象,当Looper发现有Message的时候接着得到Intent对象通过在onHandleIntent((Intent)msg.obj)中调用你的处理程序,处理完后即会停止自己的服务,意思是Intent的生命周期跟你的处理的任务是一致的,所以这个类用下载任务中非常好,下载任务结束后服务自身就会结束退出。
总结IntentService的特征有:
(1)会创建独立的worker线程来处理所有的Intent请求;
(2)会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题;
(3)所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service;
转自:http://blog.csdn.net/listening_music/article/details/7239304
Android中Service与IntentService的使用比较的更多相关文章
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- Android之Service与IntentService的比较
Android之Service与IntentService的比较 不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentServ ...
- Android中Service的使用详解和注意点(LocalService)
Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...
- Android中Service的一个Demo例子
Android中Service的一个Demo例子 Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding. 本文,主要贴代码,不对Servic ...
- android拾遗——Android之Service与IntentService的比较
不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentServic ...
- Android中Service概述
Service是Android中一种非常重要的组件,一般来说有两种用途:用Service执行长期执行的操作,而且与用户没有UI界面的交互:某个应用程序的Service能够被其它应用程序的组件调用以便提 ...
- Android中Service和Activity之间的通信
启动Service并传递数据进去: Android中通过Intent来启动服务会传递一个Intent过去. 可以在Intent中通过putExtra()携带数据 Intent startIntent ...
- Android中Service 使用详解(LocalService + RemoteService)
Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- Android中Service的使用
我个人的理解是:我们平时使用的android系统的app的后台应用,就是这个原理 可以利用Service实现程序在后台运行,依照这个原理,可以通过Service来实现关键代码的运行与实现. <一 ...
随机推荐
- 防范xss的正确姿势
防范xss的正确姿势 xss攻击是web攻击中非常常见的一种攻击手段.如果你还没有听说过xss攻击,可以先了解xss的相关知识和原理,例如: XSS)" target="_blan ...
- TCP三次握手与四次挥手过程
TCP连接的建立(三次握手) 首先,客户端与服务器均处于未连接状态,并且是客户端主动向服务器请求建立连接: 客户端将报文段中的SYN=1(同步位),并选择一个seq=x,(即该请求报文的序号为x) ...
- 【比赛游记】NOIWC2019冬眠记
上接THUWC2019酱油记. 贴一点文艺汇演的精彩表演: https://www.bilibili.com/video/av42089198/ https://www.bilibili.com/vi ...
- Windows执行命令与下载文件总结
1.前言 在渗透或是病毒分析总是会遇到很多千奇百怪的下载文件和执行命令的方法. 2.实现方式 2.1.Powershell win2003.winXP不支持 $client = new-object ...
- RESTful Web 服务:教程
RESTful Web 服务:教程 随着 REST 成为大多数 Web 和 Mobile 应用的默认选择,势必要对它的基本原理有所了解. 在它提出十多年后的今天,REST 已经成为最重要的 Web ...
- 在Eclipse使用Gradle
1.Gradle安装 1.Grandle官网下载Gradle,地址:http://www.gradle.org/downloads 2.设置环境变量,需要设置如下2个环境变量 2.1添加GRADLE_ ...
- poj1102
模拟 #include <iostream> #include <string> using namespace std; ][][] = { { ' ', '-', ' ', ...
- conding.net或github,readme.md添加图片
原因: 将图片放在仓库里面,在文件里链接它,最后 push 到 github 上.github 图片链接格式:(http://github.com/yourname/your-repositor ...
- SSIS 学习之旅 数据同步
这一章 别人也有写过但是我觉得还是写写比较好.数据同步其实就是想仿照 数据库的发布订阅功能 第一章:SSIS 学习之旅 第一个SSIS 示例(一)(上) 第二章:SSIS 学习之旅 第一个SSIS 示 ...
- css实现360导航首页超链接变色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...