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代码:

  1. public class HomeActivity extends Activity implements ResultReceiver {
  2. public void onCreate(Bundle savedInstanceState) {
  3. ...
  4. final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, QueryService.class);
  5. intent.putExtra("receiver", this);
  6. intent.putExtra("command", "query");
  7. startService(intent);
  8. }
  9. public void onReceiveResult(int resultCode, Bundle resultData) {
  10. switch (resultCode) {
  11. case RUNNING:
  12. //show progress
  13. break;
  14. case FINISHED:
  15. List results = resultData.getParcelableList("results");
  16. // do something interesting
  17. // hide progress
  18. break;
  19. case ERROR:
  20. // handle the error;
  21. break;
  22. }
  23. }
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代码:

  1. public class QueryService extends IntentService {
  2. protected void onHandleIntent(Intent intent) {
  3. final ResultReceiver receiver = intent.getParcelableExtra("receiver");
  4. String command = intent.getStringExtra("command");
  5. Bundle b = new Bundle();
  6. if(command.equals("query") {
  7. receiver.send(STATUS_RUNNING, Bundle.EMPTY);
  8. try {
  9. // get some data or something
  10. b.putParcelableArrayList("results", results);
  11. receiver.send(STATUS_FINISHED, b)
  12. } catch(Exception e) {
  13. b.putString(Intent.EXTRA_TEXT, e.toString());
  14. receiver.send(STATUS_ERROR, b);
  15. }
  16. }
  17. this.stopSelf();
  18. }
  19. }

IntentService 与ResultReceiver的更多相关文章

  1. android IntentService和ResultReceiver的异步处理

    IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...

  2. 什么时候用IntentService

    IntentService是继承自Service类的,在执行耗时操作时,其实,只需要在service中的onStartCommand(主线程)新启一个线程即可,那IntentService什么时候用来 ...

  3. IntentService

    http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...

  4. 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的

    稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...

  5. HandlerThread和IntentService

    HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...

  6. android 中IntentService的作用及使用

    IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...

  7. Android中的IntentService

    首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...

  8. Android中Services之异步IntentService

    IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务. IntentService有以下特点: (1)  它创 ...

  9. IntentService源码分析

    和HandlerThread一样,IntentService也是Android替我们封装的一个Helper类,用来简化开发流程的.接下来分析源码的时候 你就明白是怎么回事了.IntentService ...

随机推荐

  1. AC自动机算法学习

    KMP+TRIE int val[1000100][31],tot; int tr[1000100]; int fail[1000100]; struct AC_Trie{ void clean(){ ...

  2. 关于Ocelot 网关结合Consul实现服务转发的坑爹问题

    下面是我的网关配置来验证下Ocelot的问题,如果只是做网关转发应该还ok,但是要是结合Consul来检查并健康的转发有效服务器还是有很多弊端 关键在于通过设置 DeregisterCriticalS ...

  3. poj 2253 一条路径中的最大边 再找出最小的

    题目大意,有两只青蛙,分别在两个石头上,青蛙A想要到青蛙B那儿去,他可以直接跳到B的石头上,也可以跳到其他石头上,再从其他石头跳到B那儿,求青蛙从A到B的所有路径中最小的Frog Distance,我 ...

  4. linux文本编码格式转化 字幕处理

    在处理字幕的时候,linux的编码格式转换很烦. 步骤: 用python先判断 其编码,再用iconv 转编码,再用awk处理格式. file不能判断吗?file有时不准. 1.python判断编码 ...

  5. 011.KVM-V2V迁移

    一 虚拟化存储池 1.1 创建虚拟化存储池 [root@kvm-host ~]# mkdir -p /data/vmfs 1.2 定义存储池与目录 [root@kvm-host ~]# virsh p ...

  6. 002.KVM环境部署

    一 环境准备 1.1 查看是否支持虚拟化 [root@kvm-host ~]# grep -E 'vmx|svm' /proc/cpuinfo 注意:intel为vmx,amd为svm. 1.2 确定 ...

  7. 数据库简单练习 建表+select

    create table student ( sno int primary key, sname char(20),  sex char(2), birthday datetime, class i ...

  8. 好用的在线HTTP接口测试 - HTTP GET/POST模拟请求测试工具-ApiPost

    现在的模拟发送请求插件很多比如老外的postman等,但亲测咱们国内的 ApiPost 更好用一些,因为它不仅可以模拟发送get.post.delete.put请求,还可以导出文档,支持团队协作也是它 ...

  9. JavaScript的计时器的工作原理

    最近都在看一些JavaScript原理层面的文章,恰巧看到了jQuery的作者的一篇关于JavaScript计时器原理的解析,于是诚惶诚恐地决定把原文翻译成中文,一来是为了和大家分享,二来是为了加深自 ...

  10. Codeforces.666A.Reberland Linguistics(DP)

    题目链接 \(Description\) 给定串s,其由一个基本串后加任意多个长度为2或3的后缀串构成,要求基本串长度>4且相邻后缀串不相同.在基本串任意确定的情况下,求所有可能的后缀串. \( ...