IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们手动去控制或stopSelf()。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。

先来看一下IntentService类的源码:

public void onCreate() {
// TODO: It would be nice to have an option to hold a partial wakelock
// during processing, and to have a static startService(Context, Intent)
// method that would launch the service & hand off a wakelock. super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start(); //开启一个工作线程 mServiceLooper = thread.getLooper(); //单独的消息队列
mServiceHandler = new ServiceHandler(mServiceLooper);
}

定义一个IntentService的子类:

public class MIntentService extends IntentService {

    public MIntentService(){
super("MIntentService");
} /**
* Creates an IntentService. Invoked by your subclass's constructor.
* @param name Used to name the worker thread, important only for debugging.
*/
public MIntentService(String name) {
super(name);
} @Override
public void onCreate() {
Log.e("MIntentService--", "onCreate");
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("MIntentService--", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
protected void onHandleIntent(Intent intent) {
Log.e("MIntentService--", Thread.currentThread().getName() + "--" + intent.getStringExtra("info") );
for(int i = 0; i < 100; i++){ //耗时操作
Log.i("onHandleIntent--", i + "--" + Thread.currentThread().getName());
}
} @Override
public void onDestroy() {
Log.e("MIntentService--", "onDestroy");
super.onDestroy();
}
}

开启IntentService服务:

 public void intentClick(View v){
Intent intent = new Intent(this, MIntentService.class);
intent.putExtra("info", "good good study");
startService(intent);
}

点击按钮之后输出结果为(过滤log.e):

10-25 16:54:58.852  27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onCreate
10-25 16:54:58.852 27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onStartCommand
10-25 16:54:58.856 27135-27354/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ IntentService[MIntentService]--good good study
10-25 16:54:58.879 27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onDestroy

  Intent服务开启后,执行完onHandleIntent里面的任务就自动销毁结束,通过打印的线程名称可以发现是新开了一个线程来处理耗时操作的,即是耗时操作也可以被这个线程管理和执行,同时不会产生ANR的情况。

IntentService与Service的区别的更多相关文章

  1. IntentService和Service的区别

    整个看下来是一个Service+Thread+handle的结合体, Service:比Activity的被kill的级别低 Thread:不阻塞UI线程 Handle:队列式的消息循环 那这个玩意的 ...

  2. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

  3. angularjs中provider,factory,service的区别和用法

    angularjs中provider,factory,service的区别和用法 都能提供service,但是又有差别 service 第一次被注入时实例化,只实例化一次,整个应用的生命周期中是个单例 ...

  4. WCF 、Web API 、 WCF REST 和 Web Service 的区别

    WCF .Web API . WCF REST 和 Web Service 的区别 The .Net framework has a number of technologies that allow ...

  5. 【转载】@Component, @Repository, @Service的区别

    @Component, @Repository, @Service的区别 官网引用 引用spring的官方文档中的一段描述: 在Spring2.0之前的版本中,@Repository注解可以标记在任何 ...

  6. wcf和web service的区别

    1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单 ...

  7. REST和SOAP Web Service的区别比较

    本文转载自他人的博客,ArcGIS Server 推出了 对 SOAP 和 REST两种接口(用接口类型也许并不准确)类型的支持,本文非常清晰的比较了SOAP和Rest的区别联系! ///////// ...

  8. WCF、Web API、WCF REST、Web Service之区别

    http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-R ...

  9. AngularJS 服务 provider factory service及区别

    一.概念说明 1.服务是对公共代码的抽象,如多个控制器都出现了相似代码,把他们抽取出来,封装成一个服务,遵循DRY原则,增强可维护性,剥离了和具体表现相关的部分,聚焦于业务逻辑或交互逻辑,更加容易被测 ...

随机推荐

  1. Winform调用WebKitBrowser,基于chrome内核WebKit的浏览器控件

    在C#中,默认的WebBrowser控件默认使用的是IE的core,而IE的种种遭人吐槽的诟病使我不敢轻易使用WebBrowser,因此,打算使用Chrome的内核替换IE.Chrome的内核使用的是 ...

  2. 什么是 html 标签,html 实体

    为什么需要转换 更简了,因为有时候我们需要在浏览器页面中显示 html 标签,然而直接输出<script>alert(1)</script>,在浏览页面时将会被当作 html ...

  3. SQL Server 使用ROW_NUMBER实现的高效分页排序

    declare @pageNum int declare @pageSize int select * from (select ROW_NUMBER() over(order by a_Creati ...

  4. 配置php支持gd函数模块

    配置php支持gd函数模块 今天在联系上线源码包tttuangou 的时候,出现了对gd_info和imagecreatefromjpeg模块缺失的提示,我丈二和尚摸不着头脑,决定彻底学习一番 什么是 ...

  5. Linux回炉复习系列文章大纲

    本人最近在回炉Linux的内容,也做了很多整理,顺便也想将整理的内容分享出来. 由于该系列文章的内容主要是复习整理而来,其中绝大多数命令都是翻译和整理man或info文档总结的,另外很多地方也没有给出 ...

  6. Overfitting&Underfitting Problems

    这次根据结合Google的翻译果然速度快上许多,暂时休息,晚上在传一个exm2的随笔. 关于过度拟合下的问题 考虑从x∈R预测y的问题,下面的最左边的图显示了将\(y=\theta_0+\theta_ ...

  7. ReactiveCocoa源码解析(二) Bag容器的代码实现

    今天博客我接着上篇博客的内容来,上篇博客我们详细的看了ReactiveSwift中的Observer已经Event的代码实现.接下来我们来看一下ReactiveSwift中的结构体Bag的实现.Bag ...

  8. octomap中3d-rrt路径规划

    路径规划 碰撞冲突检测 在octomap中制定起止点,目标点,使用rrt规划一条路径出来,没有运动学,动力学的限制,只要能避开障碍物. 效果如下: #include "ros/ros.h&q ...

  9. 关于 asp.net 点击确定按钮 获取不到新值问题

    点击事件内,可以使用request.form[" kk"] 获取到值,但是this.txt.Text 确实旧值, 尼玛,居然没加isPostBack重新加载了数据 ,request ...

  10. 微信jssdk分享功能,jssdk成功调用,分享内容自定义失败

    前提:调用微信jssdk分享功能,通过微信开发者工具调试,调用正常,无任何报错信息. 问题:调用成功,且开发者工具正常显示,但是通过真机调试,分享出去后,自定义内容失效,为微信自动获取的默认内容!截止 ...