IntentService 与ResultReceiver
from://http://lyzhanghai.iteye.com/blog/947504
在google的I/O大会中关于“Writing zippy Android apps”,有讲过用IntentService的问题,但是因为API文档中对IntentService描述不是很详细,所以很少人使用IntentService。
android.app.IntentService
“IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(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.”
有很多种模式可以运用在RESTful Client(想要了解更多的RESTful Client的模式可以参见I/O大会Developing Android REST client applications,但是苦于没有具体demo可以参见),如果不是特别特别复杂的RESTful Web Service, 我们可以使用ResultReceiver 和 IntentService。
举个例子,你想从web service取一些数据:
1. 调用startService。
2. service中开始操作处理,并且通过消息告诉activity处理已经开始。
3. activity处理消息并且显示进度条
4. service完成处理并且返回给activity需要的数据。
5. activity处理数据。
6. service通过消息告诉activity处理完成,并且kill掉自己。
7. activity取得消息并且结束掉进度条。
activity代码:
- public class HomeActivity extends Activity implements ResultReceiver {
- public void onCreate(Bundle savedInstanceState) {
- ...
- final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, QueryService.class);
- intent.putExtra("receiver", this);
- intent.putExtra("command", "query");
- startService(intent);
- }
- public void onReceiveResult(int resultCode, Bundle resultData) {
- switch (resultCode) {
- case RUNNING:
- //show progress
- break;
- case FINISHED:
- List results = resultData.getParcelableList("results");
- // do something interesting
- // hide progress
- break;
- case ERROR:
- // handle the error;
- break;
- }
- }
public class HomeActivity extends Activity implements ResultReceiver {
public void onCreate(Bundle savedInstanceState) {
...
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, QueryService.class);
intent.putExtra("receiver", this);
intent.putExtra("command", "query");
startService(intent);
}
public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case RUNNING:
//show progress
break;
case FINISHED:
List results = resultData.getParcelableList("results");
// do something interesting
// hide progress
break;
case ERROR:
// handle the error;
break;
}
}
service代码:
- public class QueryService extends IntentService {
- protected void onHandleIntent(Intent intent) {
- final ResultReceiver receiver = intent.getParcelableExtra("receiver");
- String command = intent.getStringExtra("command");
- Bundle b = new Bundle();
- if(command.equals("query") {
- receiver.send(STATUS_RUNNING, Bundle.EMPTY);
- try {
- // get some data or something
- b.putParcelableArrayList("results", results);
- receiver.send(STATUS_FINISHED, b)
- } catch(Exception e) {
- b.putString(Intent.EXTRA_TEXT, e.toString());
- receiver.send(STATUS_ERROR, b);
- }
- }
- this.stopSelf();
- }
- }
IntentService 与ResultReceiver的更多相关文章
- android IntentService和ResultReceiver的异步处理
IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...
- 什么时候用IntentService
IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...
- IntentService
http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...
- 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的
稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...
- HandlerThread和IntentService
HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...
- android 中IntentService的作用及使用
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...
- Android中的IntentService
首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...
- Android中Services之异步IntentService
IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1) 它创 ...
- IntentService源码分析
和HandlerThread一样,IntentService也是Android替我们封装的一个Helper类,用来简化开发流程的.接下来分析源码的时候 你就明白是怎么回事了.IntentService ...
随机推荐
- Oracle ORA-12541:TNS:无监听程序
Oracle ORA-12541:TNS:无监听程序 标签: Oracle DataBase 今天使用Oracle数据库,使用可视化连接工具连接测试环境的数据库时提示无监听程序,最后在老师帮助下终于搞 ...
- SqlServer共用表达式(CTE)With As
共用表表达式(CTE)可以看成是一个临时的结果集,可以再SELECT,INSERT,UPDATE,DELETE,MARGE语句中多次引用. 一好处:使用共用表表达式可以让语句更加清晰简练. 1.可以定 ...
- hdu 2519 求组合数
求组合数 如果求C5 3 就是5*4*3/3*2*1 也就是(5/3)*(4/2)*(3/1) Sample Input5 //T3 2 //C3 25 34 43 68 0 Sample Outpu ...
- vue-cli的工程模板与构建工具
vue-cli的工程模板与构建工具 https://www.cnblogs.com/yinn/p/9712480.html 脚手架vue-cli系列二:vue-cli的工程模板与构建工具 上篇文章我们 ...
- 【LOJ】#2265. 「CTSC2017」最长上升子序列
题解 点了一个新技能叫杨表(事实上集训的时候听过,但是一直不会 这道题就是让我们找到k个不上升子序列,要求长度加和最大 我们用杨表去维护,但是由于杨表的行数可能是n的,复杂度会炸 我们只维护前\(\s ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- SpringBoot的Controller使用
一: 1.注解 2.control注解 3.效果 4.RespomseBody package com.caojun.springboot; import org.springframework.be ...
- 015.Zabbix的日志监控配置
一 日志监控概述 Zabbix可用于集中监控和分析日志,支持有日志轮询的日志监控分析.当日志中出现相关警告信息(如警告.报错等),可以发送通知给用户.日志监控功能,必须满足以下两个条件: Zabbix ...
- 深入理解Git - 一切皆commit
在对 git 有了基本理解和知道常规操作之后,如何对 git 的使用有进一步的理解? 一切皆 commit 或许是个不错的理解思路. 本文将从『一切皆 commit 』的角度,通过 git 中常见的名 ...
- mybatis generator修改默认生成的sql模板
相关连接: mybatis-generator扩展教程系列 -- 自定义sql xml文件 git项目地址