说实话,对于这个类在我实际工作中并没有用到过,通常也只是用了它的父类Service,通过官方文档可以看出类的层次结构:

而在今年的一次面试当中,有个面试官提起了它,所以虽说目前还没有真实在项目中用它,但是有必要先了解一下它的用法,在大脑中有一定的印象,以便将来真正能用到时则直接参考既可。

对于这个类的使用当然不用自己摸索,可以参考该博文:http://blog.csdn.net/hudashi/article/details/7986130

先依照官网的介绍有个大致的了解【来自银家博文】:

下面来把博文中说的例子给运行看下效果:

先声明服务MyIntentService:

public class MyIntentService extends IntentService {
final static String TAG = "cexo"; public MyIntentService() {
super("com.example.layouttest.MyIntentService");
Log.i(TAG, this + " is constructed");
} @Override
protected void onHandleIntent(Intent arg0) {
Log.i(TAG, "begin onHandleIntent() in " + this);
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, "end onHandleIntent() in " + this);
} public void onDestroy() {
super.onDestroy();
Log.i(TAG, this + " is destroy");
}
}

然后再主界面去调这个服务MainActivity:

public class MainActivity extends Activity implements OnClickListener {

    // views
private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
} @Override
public void onClick(View arg0) {
Intent intent = new Intent(this, MyIntentService.class);
startService(intent);// 连续发起了三次请求
startService(intent);
startService(intent);
} }

下面来看下运行的效果:

从运行效果可以发现跟Service的一个很大的区别在于,同时发出多个请求之后,当最后一个请求被处理,则整个Service也退出了,关于它真正的使用场景,待在实际工作中用到了再来总结,目前先学会怎么用它~

总结:

在网上搜到了一个关于IntentService的一个特点,我觉得有几点写出了它的好处:

IntentService使用的更多相关文章

  1. 什么时候用IntentService

    IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...

  2. IntentService

    http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...

  3. 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的

    稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...

  4. HandlerThread和IntentService

    HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...

  5. android 中IntentService的作用及使用

    IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...

  6. Android中的IntentService

    首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...

  7. Android中Services之异步IntentService

    IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1)  它创 ...

  8. IntentService源码分析

    和HandlerThread一样,IntentService也是Android替我们封装的一个Helper类,用来简化开发流程的.接下来分析源码的时候 你就明白是怎么回事了.IntentService ...

  9. Android Service 与 IntentService

    Service 中的耗时操作必须 在 Thread中进行: IntentService 则直接在 onHandleIntent()方法中进行

  10. Android IntentService完全解析 当Service遇到Handler

    一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...

随机推荐

  1. Java基础教程:多线程杂谈——Volatile

    Java基础教程:多线程杂谈——Volatile 引入Volatile Java语言提供了一种稍弱的同步机制,即Volatile变量,用来确保将变量的更新操作通知到其他线程.当把变量声明为Volati ...

  2. [MySQL]MySQL数据库中如何查询分组后每组中的最后一条记录?

    原文地址:https://codedefault.com/s/how-can-i-retrieve-the-last-record-in-each-group-mysql 问题描述 比如,在MySQL ...

  3. Ubuntu与Window双系统安装的注意事项

    UBUNTU与WINDOW双系统安装的注意事项   Ubuntu与Window双系统安装的注意事项 由 匿名 (未验证) 提交于 2019-05-18 10:07:41 登录 发表评论 29 次浏览 ...

  4. docker 使用阿里云镜像加速

    1.登录阿里云 2.进入控制台 3.搜索 “容器镜像服务” 下拉点击 “镜像加速器” 复制自己的私有地址 进入自己的docker宿主机器(替换下面的地址为自己的私有地址) 修改daemon配置文件/e ...

  5. poj1228(稳定凸包+特判最后一条边)

    题目链接:https://vjudge.net/problem/POJ-1228 题意:我是真的没看懂题意QAQ...搜了才知道.题目给了n个点,问这n个点确定的凸包是否能通过添加点来变成一个新的凸包 ...

  6. Linux基础-13-源码安装软件包

    1.准备 安装必要软件 yum install gcc-* glibc-* -y yum groupinstall '开发工具' -y 2.解包 tar xvf 包名 3.运行configure脚本, ...

  7. C++打印乘法表

    #include <iostream> #include <Windows.h> #include <iomanip> using namespace std; i ...

  8. Python进阶:并发编程之Futures

    区分并发和并行 并发(Concurrency). 由于Python 的解释器并不是线程安全的,为了解决由此带来的 race condition 等问题,Python 便引入了全局解释器锁,也就是同一时 ...

  9. beego入门笔记

    Beego Learn Note 示例环境在Deepin系统下. deepin 15.9.3 ├── Beego : 1.11.2 ├── GoVersion : go1.12.4 ├── GOOS ...

  10. Luogu5290 [十二省联考2019] 春节十二响 【贪心】【堆】

    题目分析: 对于一个根,假设我们对每个子树分别求出了一种答案,那么怎么合并答案是最小的呢? 首先考虑这些答案里面最大的那个数字,它肯定要融合其它组里面的最大数字.以此类推 所以最好的合并方式是,每个子 ...