本节学习IntentService, 可能就有人问了, 什么是IntentService, IntentService有什么作用? 不是已经有了Service,那为什么还要引入IntentService呢?

带着这两个问题,我们先来看一个例子:

我们新建一个MyIntentService例子:

public class MyIntentService extends IntentService {private int count = 5;public MyIntentService() {super("MyIntentService");// TODO Auto-generated constructor stub}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i("MyIntentService", "onBind------------");return super.onBind(intent);}@Overridepublic void onCreate() {// TODO Auto-generated method stubLog.i("MyIntentService", "onCreate------------");super.onCreate();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubLog.i("MyIntentService", "onDestroy------------");super.onDestroy();}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubLog.i("MyIntentService", "onStart------------");super.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.i("MyIntentService", "onStartCommand------------");return super.onStartCommand(intent, flags, startId);}@Overrideprotected void onHandleIntent(Intent arg0) {// TODO Auto-generated method stubwhile(count > 0){//设置时间的输出方式SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String  time = format.format(new Date());//显示时间Log.i("MyService", time);try {//延迟一秒Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}count--;}}}

其中IntentService是Service的子类,需要继承与Service。

在MyActivity中增加一个button,用于启动MyIntentService

     btn_intent.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// 启动服务Intent intent = new Intent(MyActivity.this, MyIntentService.class);Log.i("MyActivity", "启动Intent服务按钮被按下!");startService(intent);}});

运行效果如下:

可以看到我们的界面没有出现卡住,(关于卡住不动,请看我Service二那节)。同时打印5次后,自动destory。

既然现象已经看了,我总结一下:

Service与IntentService之间的区别

在这之前,需要知道Service的不足之处

a: Service不是专门的一条新的线程,因此不应该在Service中处理相当耗时的任务

b:service也不会专门的启动一个新的线程,Service与它所在的应用位于同一个进程中的。

所以,对于Service的2个缺陷,就引入了IntentService。

a:IntentService会使用一个队列来关联Intent的请求,每当Activity请求启动IntentService时,IntentService会将该请求加入一个队列,然后开启一个新的线程去处理请求。所以IntentService不会把主线程卡死

b:IntentService会创建单独的线程处理onHandleIntent()方法里的实现代码

c:同时IntentService不用重写onBind, OnStartCommand方法,只需实现onHandleIntent()方法

d:当所以的请求处理完后,Intent后自动停止服务,无需手动停止服务

Android 四大组件学习之Service五相关推荐

  1. Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)

    Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一.了解四大基本组件 Activity ...

  2. Android四大组件Service之AIDL详解

    Android四大组件Service之AIDL详解 前言 简介 基础知识 AIDL 服务端 定义AIDL文件规则 创建 .aidl 文件 清单注册 通过 IPC 传递对象 调用 IPC 方法 Andr ...

  3. Android 四大组件通信核心

    前言 系列文章: Android Activity创建到View的显示过程 Android 四大组件通信核心 Android 系统启动到App 界面完全展示终于明白(图文版) 我们知道Android ...

  4. Android四大组件之——Activity的生命周期(图文详解)

        转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai       联系方式:JohnTsai.Work@gmail.com       [Andro ...

  5. Android 四大组件 之 Service

    子曰:温故而知新,可以为师矣. <论语>-- 孔子 一. 简介 Android 四大组件之一,特点是无需界面,用于在后台处理耗时的操作或长期任务.甚至在程序退出的情况下,我们也可以让 Se ...

  6. android java service_[Java教程]Android四大组件之Service浅见

    [Java教程]Android四大组件之Service浅见 0 2014-04-04 18:00:28 Service 是Android四大组件之一,可以在不显示界面的情况下在后台运行.还有一个作用是 ...

  7. 下面不是android四大组件之一的是,Android四大组件之一 Service

    Service是长期在后台运行的没界面的Android四大组件之一,默认是运行在主线程中的,如果在service中执行耗时操作超过20秒没响应的话就会造成ANR,所以可以使用IntentService ...

  8. Android四大组件-Service

    目录 启动方式 独立启动 使用场景 生命周期 绑定启动 使用场景 生命周期 特性 多次启动 版本适配 5.0(21) 8.0(26) 9.0(28) 12(31) 后台启动服务思路 广播启动 省电白名 ...

  9. Android 四大组件之——Service(一)

    一.什么是服务 服务,是Android四大组件之一, 属于 计算型组件.   长期后台运行的没有界面的组件 ,特点是无用户界面.在后台运行.生命周期长 二,什么时候使用服务? 天气预报:后台的连接服务 ...

  10. Android四大组件之——Broadcast学习总结

    1.Broadcast概念 是一个全局的监听器,属于Android四大组件之一.Broadcast(广播)是一种广泛运用的在应用程序(APP)之间传输信息的机制.而BroadcastReceiver( ...

最新文章

  1. 专家点评Nature Plants | 中科院微生物所郭惠珊研究组揭示土传病原菌逃避寄主免疫的新机制...
  2. LeetCode: 38. Count and Say
  3. python flask 表单数据输出_将数据从HTML表单发送到Flask中的Python脚本
  4. 某中学的排课管理系统_某中学的排课管理系统(SQL的简单应用)
  5. Flutter开发之实现沉浸式状态栏的效果
  6. 监控系统的多协议直播(RTSP RTMP HTTP Live Streaming)
  7. C语言常量类型及名称,菜鸟带你入门C语言|基本数据类型之常量
  8. JS 仿淘宝幻灯片 非完整版 小案例
  9. Mysql 解决emoji表情处理问题 - Incorrect string value: ‘\xF0\x9F\x92\x94‘ for column
  10. data Mining with Weka: Trailer More Data Mining with Weka 用weka 进行数据挖掘 Weka 用weka 进行更多数据挖掘...
  11. 编程之美:寻找发帖水王 扩展
  12. Leetcode 刷题笔记(二十二) ——贪心算法篇之进阶题目
  13. c#textBox控件限制只允许输入数字及小数点
  14. Mysql主从知识扩展部分1
  15. Ds918 ds3615 ds3617区别_苹果678有什么区别
  16. 原生JS中动态添加元素
  17. 使用SVG做网页背景
  18. 公众号对接淘宝联盟_公众访问新联盟支持的作品
  19. 什么是软链接, 什么是硬链接
  20. 微信点击链接跳转到微信公众号关注页、微信关注链接

热门文章

  1. 安装mongodb以及设置为windows服务 详细步骤
  2. 【C++】非原创|统计代码覆盖率(一:C)
  3. 文本输入框的两种div+css的写法
  4. 在gfs2中关闭selinux
  5. C#中的HashTable 和Dictionary对象
  6. 【转载】MSDN上发现了一篇很好的WCF入门教程
  7. 配置Android应用开发环境
  8. swift开发网络篇—NSURLConnection基本使用
  9. 工具使用——印象笔记(5)
  10. DevExpress控件TExtLookupComboBox实现多列模糊匹配输入的方法