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 ...
随机推荐
- javascript-词法分析解析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Database Course Summary 001
0x01. 基本概念 SQL:Structured English Query Language 1. 数据 Data 数据(Data):描述事物的符号记录:数据内容是事物特性的反应或描述:数据是符号 ...
- Python常见面试(习题)——水仙花数
今天,给大家分享一个习题. 用python输出100到1000以内的水仙花数. 相信很多小伙伴都听到过,或者遇到过这个题目. 那么今天就来带大家做一做这道题. 首先,我们要知道什么是水仙花数, (@_ ...
- 设置idealUI选中变量的颜色与同名称变量的颜色一致
- .NetCore下B/S结构 初探基于遗传学算法的中学自动排课走班(二)
分析下染色体基因 这里用 老师 课程 班级 教室 周天 上下晚 课时作为染色体编码我封装了如下类 /// <summary> /// NP 授课事件 由教室.课程.班级 时间片段构成 li ...
- MVC插件
MVC插件 最近领导让我搞一下插件化,就是实现多个web工程通过配置文件进行组装.之前由于做过一个简单的算是有点经验,当时使用的不是area,后来通过翻看orchard源码有点启发,打算使用area改 ...
- C语言中对数组名取地址
在C/C++中,数组名相当于一个指针,指向数组的首地址.这里“相当于”不代表等于,数组名和指针还是有很多区别的,这个在<C陷阱与缺陷>里有详尽的讲述.而这里要说的是对于数组名取地址的这么一 ...
- Codeforces-1077C
title: Codeforces-1077C date: 2018-11-24 15:22:31 tags: acm 刷题 categories: Codeforces 题意 题目链接 给你一个数组 ...
- Android-多进程初识
Android-多进程初识 学习自 <Android开发艺术探索> https://baike.baidu.com/item/%E8%BF%9B%E7%A8%8B/382503?fr=al ...
- android弹出对话框
我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...