IntentService总结
我们平常写程序时经常会涉及到下载或者播放音乐等耗时操作,你可能会使用Service+Thread的方式解决,但是这样比较麻烦,如果你要串行执行多个耗时操作还要使用线程池,其实Google早就帮我们封装好了一个框架——IntentService
一、简单使用
Client端
public class ClientActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}//客户端同时发送两个任务到IntentService服务端执行public void send(View view) {Intent intent = new Intent(this, DownLoadService.class);intent.putExtra("value", "task1");startService(intent);Intent intent1 = new Intent(this, DownLoadService.class);intent1.putExtra("key", 2);intent1.putExtra("value", "task2");startService(intent1);}
}
Service端
public class DownLoadService extends IntentService {public static final String TAG = "DownLoadService";public DownLoadService() {super("DownLoadService");}@Overrideprotected void onHandleIntent(Intent intent) {String value = intent.getStringExtra("value");switch (key) {case 1://耗时任务1try {Thread.sleep(3 * 1000);} catch (InterruptedException e) {e.printStackTrace();}break;case 2://耗时任务2try {Thread.sleep(3 * 1000);} catch (InterruptedException e) {e.printStackTrace();}break;default:break;}}
}
因为它本身还是一个Service所以还需要在AndroidManfiest中进行注册,
需要注意的点:
- 只要耗时操作一执行完它就会停止
- 只能通过startService开启
二、源码分析
1.构造方法
public IntentService(String name) {super();mName = name;}
传入的name是内部线程的名字
2.onCreate()
public void onCreate() {super.onCreate();HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");thread.start();mServiceLooper = thread.getLooper();mServiceHandler = new ServiceHandler(mServiceLooper);}
该方法首先创建了一个HandlerThread,并调用了它的start方法,然后实例化了一个Handler并与HandlerThread的Looper绑定
3.onStartCommand()
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {onStart(intent, startId);return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;}
在里面调用了onStart方法,在IntentService中你无需重写此方法
@Overridepublic void onStart(Intent intent, int startId) {Message msg = mServiceHandler.obtainMessage();msg.arg1 = startId;msg.obj = intent;mServiceHandler.sendMessage(msg);}
在onStart里创建了一个Message,并将intent赋值给它的obj属性,使用Handler发送消息,所以说必须使用startService启动IntentService
private final class ServiceHandler extends Handler {public ServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {onHandleIntent((Intent)msg.obj);stopSelf(msg.arg1);}}
在Handler的handleMessage中处理消息,将消息传给onHandleIntent处理,onHandleIntent也是抽象方法,你必须重写此方法来对不同的intent进行不同的处理,处理完后调用stopSelf结束自己
4.onDestory()
@Overridepublic void onDestroy() {mServiceLooper.quit();}
最后调用quit停止looper
三、总结
首先在IntentService的构造方法里面你会看到创建了 一个HandlerThread线程出来,并且调用了它的start方法启动了该线程,之前在HandlerThread总结中已经讲过会在该线程的run方法里面创建Looper对象并且调用loop将Looper运转起来,接着会通过创建的Looper对象创建一个ServiceHandler出来,其实就是Handler对象而已,该对象里面有handleMessage方法,在我们通过startService方法启动IntentService的时候 会回调onStartCommand方法,该方法会执行IntentService的onStart方法,而正是在onStart方法里面会将我们 startService传入的intent对象封装成Message对象通过在构造函数中创建的ServiceHandler类型handler对象的sendMessage方法发送出去,那么紧接着就会回调ServiceHandler的handleMessage方法了,handleMessage方法实际上执行的是onHandleIntent方法,也就是我们在实现IntentService抽象类的时候需要实现的方法,具体实现对Intent的操作,操作结束之后handleMessage方法会执行stopSelf方法结束当前IntentService;
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方法实现的代码, 隐藏开发者无须处理多线程问题, 当所有请求 ...
最新文章
- wxWidgets:wxFloatingPointValidator<T> 类模板用法
- centos 删除crontab_centos7 定时任务crontab命令详解
- android10获取imei,Android 10 root用户获取imei
- 创建字节跳动之前,张一鸣读过哪些硬核技术书?
- JSON与Delphi Object的互换
- oracle和mysql数据实时同步_Canal 实现 Mysql数据库实时数据同步
- C# C++ 共享内存 结构体读写 结构体中嵌套结构体 结构体中带string Char*的处理方式
- 2021新版外卖CPS 外卖侠CPS小程序
- Microsoft® Silverlight™ Streaming by Windows Live™
- 电信跨域跨系统业务实践
- 计算机审计中级培训班,计算机审计中级培训心得体会
- VTK笔记-组建vtkPolyData-四面体/五面体/六面体
- Python通过IMAP实现邮箱客户端
- Promise中then的执行顺序详解
- [转载]一位也许是真正的hack说的话
- 什么是APS系统?其重要功能有哪些?这篇文章写得很清楚
- 百度地图LBS应用开发代码
- 敏捷物联——引领生产和服务创新
- stm32心率监测系统(心率监测,wifi上传,APP显示,上位机显示)
- 基于TensorFlow实现预测房价--回归问题
热门文章
- dw按钮图片滚动js_js实现图片无缝滚动特效
- Android开发本地及网络Mp3音乐播放器(十三)网络音乐搜索功能实现,歌名歌手专辑名搜索
- 可用性设计,选你所爱,爱你所选
- 赛迪顾问中国信息安全产品市场研究年度报告2010摘要
- Xplorer² v2.2.0.0 Ultimate x86
- 为什么很多公司转型 Go 语言开发?Go 语言能做什么
- DeepMind提出关系RNN:构建关系推理模块,强化学习利器
- 如何提取Redis中的大KEY
- PVCBOT【20号】机械螃蟹--横向爬行机器人
- 64位php 套装 apache,WIN764位下 PHP7.3 apache2.4 安装配置