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");}@Overrideprotected 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 {// viewsprivate Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View arg0) {Intent intent = new Intent(this, MyIntentService.class);startService(intent);// 连续发起了三次请求 startService(intent);startService(intent);}}
下面来看下运行的效果:
从运行效果可以发现跟Service的一个很大的区别在于,同时发出多个请求之后,当最后一个请求被处理,则整个Service也退出了,关于它真正的使用场景,待在实际工作中用到了再来总结,目前先学会怎么用它~
总结:
在网上搜到了一个关于IntentService的一个特点,我觉得有几点写出了它的好处:
转载于:https://www.cnblogs.com/webor2006/p/4305715.html
IntentService使用相关推荐
- 剖析IntentService的运作机理
2019独角兽企业重金招聘Python工程师标准>>> 剖析IntentService的运作机理 (本文以Android 5.1为准) 侯 亮 1 概述 在讲述Service机制的文 ...
- HandlerThread和IntentService源码解析
简介 首先我们先来了解HandlerThread和IntentService是什么,以及为什么要将这两者放在一起分析. HandlerThread: HandlerThread 其实是Handler ...
- android intent 源码,Android 基础之 IntentService 源码
Android 基础之 IntentService 源码 Android,IntentService,源码 IntentService 位于 android.app 包下面,是 Service 的一个 ...
- Android开发之Service与IntentService的区别与使用场景(源代码剖析)
因为最大部分的service不需要同时处理多个请求(处理多个请求是一个比较危险的多线程的场景),这样在在这种情况下呢,最好使用IntentService类如果你实现你的服务. 使用intentServ ...
- Handler消息机制(九):IntentService源码解析
作者:jtsky 链接:https://www.jianshu.com/p/0a150ec09a32 简介 首先我们先来了解HandlerThread和IntentService是什么,以及为什么要将 ...
- Service IntentService区别 (面试)
依然记得自己当初没有真正的工作经验的时候的日子,满北京跑,没有人要.妈的,现在就想问,还有谁!想想真解气.不提了. 曾经有个面试官问我service 和IntentService的区别.当时自己模模糊 ...
- 在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast
在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast 1. 表象 Service中可以正常显示Toast,IntentServic ...
- 使用IntentService给自己的Android应用写一个文件下载器。
接着上一篇的http://www.cnblogs.com/zhengxt/p/3657833.html,当我们想给自己的APP写一个文件下载器时,可以用重写IntentService来实现. 使用In ...
- IntentService
http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...
- android 发送前台广播,使用IntentService与BroadcastReceiver实现后台服务(Android7.0可用)...
IntentService的优点 IntentService会创建单独的线程处理所有的Intent请求, 会处理onHandleIntent方法实现的代码, 隐藏开发者无须处理多线程问题, 当所有请求 ...
最新文章
- 多层科目任意组合汇总报表的性能优化 (上)
- 软件工程 工具之二—— PowerDesigner v12(二)
- 多波次导弹发射中的规划问题(二) 问题一解答
- NodeJs中process.cwd()与__dirname的区别
- 「后端小伙伴来学前端了」Vue中Props 实现组件通信TodoList案例
- live http headers_iPhone12手机Live+利用VJD2导播软件直播
- Spring BeanDefinition加载
- boost asio resolver
- 【Unity Shaders】Lighting Models —— 灯型号Lit Sphere
- 能写个简单的oa系统java_基于jsp的轻量级OA系统-JavaEE实现轻量级OA系统 - java项目源码...
- mysql if / case / limit / join / 数据类型 、oracle decode 及其它sql对比
- 2021年中职“网络安全“江西省赛题—B-8:Web渗透测试
- 中国大学MOOC-陈越、何钦铭-数据结构-习题解答-02 线性结构
- 谷歌阻止苹果,谁来阻止谷歌
- 【中文技术文档的写作规范_P03】如何书写标点符号和控制文档体系
- 计算机网络(谢希仁-第八版)第五章习题全解
- 贝壳一键还原1.0 使用详解
- 最全软件著作权申请流程
- 热像仪和夜视仪的区别
- Android版本新特性总结