- 浏览: 924283 次
- 性别:
- 来自: 重庆
文章分类
- 全部博客 (453)
- Windows phone 7 (0)
- Andriod (47)
- iPhone (1)
- Web (30)
- J2EE (34)
- stucts 2学习笔记 (34)
- 开发项目 (3)
- spring学习笔记 (24)
- EJB学习笔记 (6)
- Hibernate学习笔记 (15)
- JPA学习笔记 (8)
- Jsp (11)
- ajax (7)
- 异常收集模块 (1)
- jquery (2)
- phoneGap (2)
- jquery Mobile (0)
- java面试总结 (5)
- Object-C (0)
- javascript (6)
- Eclipse (5)
- 支付集成 (2)
- Weblogic (1)
- Dubbox (5)
- Redis (10)
- linux (21)
- Codis (2)
- React Native (0)
- Mysql (6)
- Docker (3)
- 自动化部署 (1)
- 项目Bug管理平台 (2)
- 负载均衡 (1)
- Mycat (2)
- Java基础知识 (16)
- 数据库 (7)
- Maven (17)
- Kafka (21)
- logs (2)
- 大并发 (2)
- 消息中间件 (2)
- 分布式锁 (3)
- 算法 (4)
- 数字证书原理,公钥私钥 (0)
- 数字证书原理 (1)
- 公钥私钥 (1)
- zookeeper (4)
- Hbase (9)
- Hadoop (2)
- storm (2)
- 通信协议 (1)
- Hive (3)
- git (1)
- JVM (2)
- 大数据相关算法 (1)
- idea (5)
- 将博客搬至CSDN (1)
- 设计模式 (2)
- 表达式 (1)
- 代码审查工具 (0)
- 开源项目 (1)
- PyCharm (0)
- python (6)
- Kubernetes (1)
- swagger (1)
- Maven中mirrors和repository的关系 (0)
- RabbitMQ (3)
- redisson (1)
- k8s (2)
- Mac (1)
最新评论
-
misisipi101:
假设库已经分为32个,那么要扩展到64个,怎样做呢
订单分库分表实践总结以及关键步骤 -
mfkxk298:
简单明了的例子,解决了问题,谢谢啦!
关于ListView中notifyDataSetChanged()刷新数据不更新原因 -
whbwang:
" target="_blank" ...
java web开发 高并发处理 -
suguoqian:
...
java web开发 高并发处理 -
xiangnanyujing:
Dubbox+Redis3.0+Spring+Hibernate+zookeeper实现消息推送核心搭建
不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentService的解释,发现了它相对于Service来说有很多更加方便之处,今天在这里稍微来总结下我的心得。
首先IntentService是继承自Service的,那我们先看看Service的官方介绍,这里列出两点比较重要的地方:
1.A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
2.A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
稍微翻一下(英文水平一般)
1.Service不是一个单独的进程 ,它和应用程序在同一个进程中。
2.Service不是一个线程,所以我们应该避免在Service里面进行耗时的操作
关于第二点我想说下,不知道很多网上的文章都把耗时的操作直接放在Service的onStart方法中,而且没有强调这样会出现Application Not Responding!希望我的文章能帮大家认清这个误区(Service不是一个线程,不能直接处理耗时的操作)。
有人肯定会问,那么为什么我不直接用Thread而要用Service呢?关于这个,大家可以网上搜搜,这里不过多解释。有一点需要强调,如果有耗时操作在Service里,就必须开启一个单独的线程来处理!!!这点一定要铭记在心。
IntentService相对于Service来说,有几个非常有用的优点,首先我们看看官方文档的说明:
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests throughstartService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
1.Service:
|
2.IntentService:
|
测试主程序:
|
发表评论
-
App开发一些总结
2016-04-05 17:48 2559架构因人而异,不同的架构师大多会有不同的看法;架构也因项目 ... -
java 实现微信搜索附近人功能
2015-03-25 18:07 4868最近给andorid做后台查询数据功能,有一个需求是模仿微信 ... -
重庆APP开发 重庆Android 重庆Ios 爬虫科技 重庆爬虫科技
2014-10-06 12:03 9<!--[if gte mso 9]><x ... -
android 操作文件
2012-08-30 11:15 1024package com.liyong; import jav ... -
android获取短信内容
2012-08-29 23:10 1500自己研究: package com.liyong.data; ... -
Android中的日历操作
2012-08-29 13:01 2987日历的操作方法,这里主要用到了ContentProiver的知 ... -
android获取CMNET、WIFI、CMWAP联网状态
2012-08-29 12:40 5839private static final int CMNET= ... -
android实现手势缩放、移动图片
2012-08-28 19:37 9224新建一个触摸监听器类 package com.liyong. ... -
怎样把信息绘制到图片中并且保存到本地硬盘上
2012-08-27 12:53 1326自己研究吧。。。 //解释一下由于我们不是绘制到手机屏幕上所 ... -
关于ListView中notifyDataSetChanged()刷新数据不更新原因
2012-08-26 21:41 50267使用Listview的时候: 当要动态显示更改后的数据(例如数 ... -
android中获取联系人相关信息
2012-08-26 18:42 1641自己看........... public static ... -
修改Android中strings.xml文件, 动态改变数据
2012-08-24 18:25 10810有些朋友可能会动态的修改Android中strings.xml ... -
android内存泄漏
2012-07-25 21:04 1676尽量不要使用setImageBitmap或setImageRe ... -
将程序安装到SD卡
2012-04-28 22:11 859Android 2.2系统的一大改进就是通过Move to S ... -
一个很好的加密解密字符串
2012-04-28 22:11 1121package net.sf.andhsli.hotspotl ... -
获取手机位置
2012-04-28 22:11 983private double[] getGPS() { Lo ... -
获取手机位置
2012-05-04 09:39 1051private double[] getGPS() { Lo ... -
查看内存存储
2012-05-03 10:05 1054import java.io.File; import a ... -
获取本机电话号码
2012-05-03 10:04 1089private String getMyPhoneNumber ... -
发送彩信
2012-05-03 10:04 975Uri uri = Uri.parse(\"cont ...
相关推荐
service中1需要手动开启子线程2服务开启之后会一直运行,需要手动调用stopService();或者stopSelf(); intentService是一种异步(子线程)、自动停止的服务,这个例子测试IntentService的特征
介绍service和intentservice的使用和其中应该注意的部分
主要介绍了Android系统中Service和IntentService的区别,与普通的服务相比,IntentService可以开启单独的线程来处理intent请求,需要的朋友可以参考下
3. 在ServiceDemoActivity.java中都调用了两个service,调用service自行屏蔽调用IntentServiceServie,调用IntentServiceServie自行屏蔽调用service。 4. 仅仅是个例子,对比这个service和IntentServiceServie的区别...
NULL 博文链接:https://zhanglimijavaeye.iteye.com/blog/1881599
Android中的IntentService是继承自Service类的,在我们讨论IntentService之前,我们先想一下Service的特点: Service的回调方法(onCreate、onStartCommand、onBind、onDestroy)都是运行在主线程中的。当我们通过start...
1)Service默认运行在主线程中,IntentService运行在一个新的线程中 2)Service需要主动调用stopSelf()或stopService()服务才可以停止,IntentService运行完后自动停止 使用IntentService需要注意2点: 1)构造函数中...
Service不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中; Service也不是专门一条新线程,因此不应该在Service中直接处理耗时的任务; 二、IntentService特征 会创建独立的worker线程来...
IntentService基本使用方法Demo
IntentService简单demo,对应博文 http://blog.csdn.net/tianmi1988/article/details/50520339
Android 中IntentService的简单应用
对于有写工作,我们需要在后台进行,这里主要介绍了使用IntentService轻松实现模拟上传图片的例子。对于IntentServixce和Service扩展类的使用区别,大家可以自行去了解研究一下。
主要介绍了Android中使用IntentService创建后台服务实例,IntentService提供了在单个后台线程运行操作的简单结构,需要的朋友可以参考下
Android四大组件——Service后台服务、前台服务、IntentService、跨进程服务、无障碍服务、系统服务
Android说明Service生命周期的代码例子。用于演示Service组件从创建到销毁的完整生命周期,以及IntentService的使用例子。
本篇文章主要介绍了Android 基于IntentService的文件下载的示例代码,具有一定的参考价值,有兴趣的可以了解一下
Android_Blog_Demos 存储CSDN博客的一些源码...Android IntentService完全解析 当Service遇到Handler 详细 Android 高清加载巨图方案 拒绝压缩图片 ViewDragHelper实战 自己打造Drawerlayout Android UI性能优化实