android 中IntentService的使用场景
IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们手动去控制或stopSelf()。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
在Android开发中,我们或许会碰到这么一种业务需求,一项任务分成几个子任务,子任务按顺序先后执行,子任务全部执行完后,这项任务才算成功。那么,利用几个子线程顺序执行是可以达到这个目的的,但是每个线程必须去手动控制,而且得在一个子线程执行完后,再开启另一个子线程。或者,全部放到一个线程中让其顺序执行。这样都可以做到,但是,如果这是一个后台任务,就得放到Service里面,由于Service和Activity是同级的,所以,要执行耗时任务,就得在Service里面开子线程来执行。那么,有没有一种简单的方法来处理这个过程呢,答案就是IntentService。
什么是IntentService,首先看看官方的解释:
IntentService is a base class forServices that handle asynchronous requests (expressed asIntents) 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
简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
还有一个说明是:
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.
大致意思是:所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一个请求。
那么,用IntentService有什么好处呢?首先,我们省去了在Service中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止Service,第三,it's so easy to use!
package im.weiyuan.com.viewutils; import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log; /**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* TODO: Customize class - update intent actions, extra parameters and static
* helper methods.
*/
public class MyIntentService extends IntentService { /**
* 必须要写一个无参数的构造函数,然后调用父类的 super("MyIntentService");
* 其中MyIntentService就是执行onHandleIntent对应的线程的名字
* */
public MyIntentService() {
super("MyIntentService");
} /**
* onHandleIntent函数是在子线程中去执行处理的,所以这里就没有必要去开启线程
* */ @Override
protected void onHandleIntent(Intent intent) {
Log.d("123456","onHandleIntent is called");
/**
* 模拟耗时操作
* */
SystemClock.sleep(10000);
Log.d("123456","onHandleIntent is out");
} /**
* onHandleIntent函数中的耗时任务执行完成后,服务会自动销毁
* 调用onDestroy函数
*
* */ @Override
public void onDestroy() {
super.onDestroy();
Log.d("123456","onDestroy is called");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="im.weiyuan.com.viewutils"> <permission android:name="com.weiyuan.sb" /> <uses-permission android:name="com.weiyuan.sb" /> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service
android:name=".MyIntentService"
android:exported="false"></service>
</application> </manifest>
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
我们来看看日志打印的输出:
07-23 11:52:18.507 14993-15238/im.weiyuan.com.viewutils D/123456: onHandleIntent is called
07-23 11:52:28.508 14993-15238/im.weiyuan.com.viewutils D/123456: onHandleIntent is out
07-23 11:52:28.510 14993-14993/im.weiyuan.com.viewutils D/123456: onDestroy is called
不清楚的看博客:
http://blog.csdn.net/ryantang03/article/details/8146154/
相当的经典
android 中IntentService的使用场景的更多相关文章
- android 中IntentService的作用及使用
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...
- Android中IntentService与Service
Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独 ...
- Android中IntentService的原理及使用
在Android开发中,我们或许会碰到这么一种业务需求,一项任务分成几个子任务,子任务按顺序先后执行,子任务全部执行完后,这项任务才算成功.那么,利用几个子线程顺序执行是可以达到这个目的的,但是每个线 ...
- [Android Pro] Android中IntentService的原理及使用
转载自:http://blog.csdn.net/ryantang03/article/details/8146154 在Android开发中,我 们或许会碰到这么一种业务需求,一项任务分成几个子任务 ...
- Android中IntentService详解
简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service ...
- Android 中 IntentService 的优点
简而言之:可以处理异步请求,任务完成会自动停止自己. IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的Service,使用时你只需要 ...
- RxJava(九)zip操作符在Android中的实际使用场景
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51614927 本文出自:[余志强的博客] 一.zip操作符概述 官方 ...
- 系统剖析Android中的内存泄漏
[转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...
- Android中直播视频技术探究之---摄像头Camera视频源数据采集解析
一.前言 在视频直播中一般都是两种视频数据源,一个是摄像头数据,一个是录制桌面数据,而一般来说美女妹子直播都是来自于摄像头数据,游戏直播都是录制桌面数据的,那么今天就来看看第一个数据源数据采集分析,A ...
随机推荐
- Eclipse中常用快捷键的使用
1.补全代码的声明:alt + / 2.快速修复: ctrl + 1 3.批量导包:ctrl + shift + o 4.使用单行注释:ctrl + / 5.使用多行注释: ctrl + shift ...
- Rocket - tilelink - AsyncCrossing
https://mp.weixin.qq.com/s/v8plWCBD8vZkxykjJe4TCg 介绍AsyncCrossing的实现,主要介绍如何实现diplomacy Node和LazyMo ...
- Linux (三) 基础命令 上
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一 . 背景知识 1. Linux系统中一切皆文件 在Linux系统中任何东西都是以文件形式来存储的.这 ...
- Java实现 蓝桥杯 算法训练 相邻数对(暴力)
试题 算法训练 相邻数对 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 ...
- Java实现洛谷 P1072 Hankson 的趣味题
P1072 Hankson 的趣味题 输入输出样例 输入 2 41 1 96 288 95 1 37 1776 输出 6 2 PS: 通过辗转相除法的推导 import java.util.*; cl ...
- 流程图(HTML5拖拽)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- iOS -实现UIView圆角显示的方法
添加一个UIView时,默认直角显示有时对于有强迫症的患者还真有点不舒服! eg: 怎么实现UIview的圆角显示呢? 首先包含一个头文件: #import <QuartzCore/Quartz ...
- jenkins+svn 自动化上线
一.环境: [root@bimvm01 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@bimvm01 ~ ...
- springMVC 异常
springMVC 异常 0.依赖(不只是本次案例所需) <?xml version="1.0" encoding="UTF-8"?> <p ...
- 5、vue的传值
1-父组件向子组件 -父组件向孙子组件传值(属性传值) //父组件<template> <div id='app'> <headera v-bind:hea=" ...