说实话,对于这个类在我实际工作中并没有用到过,通常也只是用了它的父类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. 接口请求报错 504 Gateway Time-out

    最近程序接口请求报了一个错误,如图 很明显的请求超时,以前也没出现过这个问题,突然就报了这个错,很懵. 百度之后网上说是nginx的问题,然后突然想起来,因为业务需要我在nginx里配了接口的转发. ...

  2. bootstrap-table中使用bootstrap-switch开关按钮

    先上图 准备工作: 添加css和js文件 #bootstrap开关按钮#} <link href="https://cdn.bootcss.com/bootstrap-switch/3 ...

  3. vue-cli3.0和element-ui及axios的安装使用

    一.利用vue-cli3快速搭建vue项目 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cli 全局安装的 npm 包,提供了终端里的vue命令( ...

  4. mysql索引 多个单列索引和联合索引的区别详解

    背景: 为了提高数据库效率,建索引是家常便饭:那么当查询条件为2个及以上时,我们是创建多个单列索引还是创建一个联合索引好呢?他们之间的区别是什么?哪个效率高呢?我在这里详细测试分析下. 一.联合索引测 ...

  5. win10改装win7

    参考链接: https://jingyan.baidu.com/article/3ea51489d0f3c852e61bba01.html 1. 制作win7 U盘启动盘 2. 设置BIOS 1:OS ...

  6. Reids原理之IO模型

    众所周知Redis是单进程单线程的应用,在如今多核横行的时代,我们不免有疑问,单线程的redis怎么就成了高性能的代表 当有多个线程同时调用redis的时候,那么单线程的redis是怎么处理的呢,这里 ...

  7. Python09之range函数(BIF内置函数)

    具体语法: range(起始值,结束值,步进值) range() 其属于内置函数,不需要导入其他模块即可使用,直接在Python的IDLE直接可以使用. list(range(0,10)) [0, 1 ...

  8. 2019ICPC南昌现场赛总结

    非常可惜的一场比赛,多了60分钟罚时与银牌无缘.今年6场ICPC网络赛里面打的最差的就是南昌站,冥冥之中自有天意吧,最后被安排去了南昌. 开场被队友叫去先看的L,说是足球,发现就是简单模拟,就直接上机 ...

  9. 随记sqlserver学习笔记(一)

    create database testuse test --部门表create table department( dept_id int not null identity primary key ...

  10. pycharm 使用black

    pycharm 使用black The Uncompromising Code Formatter By using Black, you agree to cede control over min ...