[Android Pro] Android中IntentService的原理及使用
转载自:http://blog.csdn.net/ryantang03/article/details/8146154
在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!
ok,接下来让我们来看看如何使用,我写了一个Demo来模拟两个耗时操作,Operation1与Operation2,先执行1,2必须等1执行完才能执行:
新建工程,新建一个继承IntentService的类,我这里是IntentServiceDemo.java
public class IntentServiceDemo extends IntentService {
public IntentServiceDemo() {
//必须实现父类的构造方法
super("IntentServiceDemo");
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return super.onBind(intent);
}
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
System.out.println("onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void setIntentRedelivery(boolean enabled) {
super.setIntentRedelivery(enabled);
System.out.println("setIntentRedelivery");
}
@Override
protected void onHandleIntent(Intent intent) {
//Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务
String action = intent.getExtras().getString("param");
if (action.equals("oper1")) {
System.out.println("Operation1");
}else if (action.equals("oper2")) {
System.out.println("Operation2");
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
}
我把生命周期方法全打印出来了,待会我们来看看它执行的过程是怎样的。接下来是Activity,在Activity中来启动IntentService:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个
//Operation 1
Intent startServiceIntent = new Intent("com.test.intentservice");
Bundle bundle = new Bundle();
bundle.putString("param", "oper1");
startServiceIntent.putExtras(bundle);
startService(startServiceIntent);
//Operation 2
Intent startServiceIntent2 = new Intent("com.test.intentservice");
Bundle bundle2 = new Bundle();
bundle2.putString("param", "oper2");
startServiceIntent2.putExtras(bundle2);
startService(startServiceIntent2);
}
}
最后,别忘了配置Service,因为它继承于Service,所以,它还是一个Service,一定要配置,否则是不起作用的,开始我就是忘了,结果半天没反应。
<service android:name=".IntentServiceDemo">
<intent-filter >
<action android:name="com.test.intentservice"/>
</intent-filter>
</service>
ok,最后来看看执行结果:

从结果可以看
到,onCreate方法只执行了一次,而onStartCommand和onStart方法执行了两次,开启了两个Work
Thread,这就证实了之前所说的,启动多次,但IntentService的实例只有一个,这跟传统的Service是一样的。Operation1
也是先于Operation2打印,并且我让两个操作间停顿了2s,最后是onDestroy销毁了IntentService。
这就是IntentService,一个方便我们处理业务流程的类,它是一个Service,但是比Service更智能。
[Android Pro] Android中IntentService的原理及使用的更多相关文章
- Android中IntentService的原理及使用
在Android开发中,我们或许会碰到这么一种业务需求,一项任务分成几个子任务,子任务按顺序先后执行,子任务全部执行完后,这项任务才算成功.那么,利用几个子线程顺序执行是可以达到这个目的的,但是每个线 ...
- Android 7.0 中 ContentProvider 实现原理
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文描述了ContentProvider发布者和调用者这两在Framework层是如何实现的. 作为Android的四大 ...
- [Android Pro] android中permission_group与permisson区别、作用
转载:http://blog.csdn.net/feng88724/article/details/6409313 其实Android在定义 permission 时, 为每个Permission都进 ...
- [Android Pro] service中显示一个dialog 或者通过windowmanage显示view
转载: http://blog.csdn.net/huxueyan521/article/details/8954844 通过windowmananger来在窗口上添加view的时候,需要设置aler ...
- [Android Pro] Android 4.1 使用 Accessibility实现免Root自动批量安装功能
reference to : http://www.infoq.com/cn/articles/android-accessibility-installing?utm_campaign=info ...
- [Android Pro] Android 4.3 NotificationListenerService使用详解
reference to : http://blog.csdn.net/yihongyuelan/article/details/40977323 概况 Android在4.3的版本中(即API 18 ...
- [Android Pro] android root权限破解分析
许 多机友新购来的Android机器没有破解过Root权限,无法使用一些需要高权限的软件,以及进行一些高权限的操作,其实破解手机Root权限是比较简 单及安全的,破解Root权限的原理就是在手机的/s ...
- [Android Pro] Android 手机root 并 安装 BusyBox pro 和 Android Terminal Emulator
Android root 工具:http://www.z4root.cn/yijianrootshouji/ 推荐的是:root精灵手机版 BusyBox 称为 Linux 工具里的瑞士军刀.简单的说 ...
- [Android Pro] Android开发实践:自定义ViewGroup的onLayout()分析
reference to : http://www.linuxidc.com/Linux/2014-12/110165.htm 前一篇文章主要讲了自定义View为什么要重载onMeasure()方法( ...
随机推荐
- 1975: [Sdoi2010]魔法猪学院
k短路,只会写A*的飘过..priority_queue超空间差评!
- 【POJ】1067.取石子游戏
题解 这道题让我对SG函数有了更深刻的理解,这是道打表找规律题 我们打出来SG函数似乎是 1 2必败 3 5必败 4 7必败 6 10必败 8 13必败 哇我找到规律了-- 然而,我显然不会通项 后来 ...
- 终端(terminal)、tty、shell、控制台(console)、bash之间的区别与联系
1.终端(terminal) 终端(termimal)= tty(Teletypewriter, 电传打印机),作用是提供一个命令的输入输出环境,在linux下使用组合键ctrl+alt+T打开的就是 ...
- C#中 EF(EntityFramework) 性能优化
现在工作中很少使用原生的sql了,大多数的时候都在使用EF.刚开始的时候,只是在注重功能的实现,最近一段时间在做服务端接口开发.开发的时候也是像之前一样,键盘噼里啪啦的一顿敲,接口秒秒钟上线,但是到联 ...
- Git 码云操作
https://www.cnblogs.com/lanxiang/p/7487971.html 将项目提交到码云 在码云创建项目 然后在项目目录下: 打开后输入: git init 目录下出现.git ...
- hdu-5023线段树刷题
title: hdu-5023线段树刷题 date: 2018-10-18 13:32:13 tags: acm 刷题 categories: ACM-线段树 概述 这道题和上次做的那道染色问题一样, ...
- 不要62 hdu 2089 dfs记忆化搜索
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...
- 在qemu环境中用gdb调试Linux内核
简介 对用户态进程,利用gdb调试代码是很方便的手段.而对于内核态的问题,可以利用crash等工具基于coredump文件进行调试.其实我们也可以利用一些手段对Linux内核代码进行gdb调试,qem ...
- Eclipse 更改默认的编码 和 换行符
- [leetcode tree]98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...