IntentService使用
说实话,对于这个类在我实际工作中并没有用到过,通常也只是用了它的父类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使用的更多相关文章
- 什么时候用IntentService
IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...
- IntentService
http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...
- 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的
稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...
- HandlerThread和IntentService
HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...
- android 中IntentService的作用及使用
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...
- Android中的IntentService
首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...
- Android中Services之异步IntentService
IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1) 它创 ...
- IntentService源码分析
和HandlerThread一样,IntentService也是Android替我们封装的一个Helper类,用来简化开发流程的.接下来分析源码的时候 你就明白是怎么回事了.IntentService ...
- Android Service 与 IntentService
Service 中的耗时操作必须 在 Thread中进行: IntentService 则直接在 onHandleIntent()方法中进行
- Android IntentService完全解析 当Service遇到Handler
一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...
随机推荐
- Xmanager教程
简介 Xmanager是市场上领先的PC X服务器,可将X应用程序的强大功能带入Windows环境. 提供了强大的会话管理控制台,易于使用的X应用程序启动器,X服务器配置文件管理工具,SSH模块和高性 ...
- JDBC(连接数据库的四个主要步骤)
JDBC连接数据库 ?创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.l ...
- PHP使用glob方法遍历文件夹下所有文件
PHP使用glob方法遍历文件夹下所有文件 遍历文件夹下所有文件,一般可以使用opendir 与 readdir 方法来遍历.<pre><?php$path = dirname(__ ...
- pycharm设置开发模板/字体大小/背景颜色(3)
一.pycharm设置字体大小/风格 选择 File –> setting –> Editor –> Font ,可以看到如上界面,可以根据自己的喜好随意调整字体大小,字体风格,文字 ...
- DataGridView中的Combobox的应用
在WinForm中DataGridView可谓是应用比较多的数据显示控件了,DataGridView中可以应用各种控件,关于这样的文章网上 已有很多.都是实例化一个控件然后通过DataGridView ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- 有助提升编程的几个Python 技巧
一行代码定义List 定义某种列表时,写For 循环过于麻烦,幸运的是,Python有一种内置的方法可以在一行代码中解决这个问题. 下面是使用For循环创建列表和用一行代码创建列表的对比. x = [ ...
- 外网访问虚拟机搭建的web服务
凌晨了,就简单写个一定可行的思路吧,有时间了再补上. 设置虚拟机为桥接模式,当然NAT也行,只是我嫌NAT麻烦 设置路由器,将虚拟机端口映射到外网
- 长乐培训Day2
T1 足球联赛 题目 [题目描述] 巴蜀中学新一季的足球联赛开幕了.足球联赛有n只球队参赛,每赛季,每只球队要与其他球队各赛两场,主客各一场,赢一场得3分,输一场不得分,平局两只队伍各得一分. 英勇无 ...
- Android去评分,分享
去评分: 跳到手机中已安装的市场评分页面 Uri uri = Uri.parse("market://details?id="+getPackageName()); Intent ...