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. Linux 管道

    管道命令 " | ",竖线符号代表的就是管道符 管道是一种两个进程间进行单向通信的机制.因为管道传递数据的单向性,所以又称为半双工管道. 介绍: 管道可以根据一组命令按照数据流向的 ...

  2. Java基础86 MySQL数据库基础知识

    本文知识点(目录): 1.MySQL数据库的概述    2.MySQL数据库的管理[对数据库的操作](查询.删除.创建数据库,以及查询和修改数据库的编码模式)    3.表的管理[对数据库 表的操作] ...

  3. Java 接口关键字 interface

    interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体的实现,它允许创建者确定方法名.参数列表和返回类型,但没有任何方法体,接口只提供了形式,而未提供任何具体实现 一个接口表示:& ...

  4. 【转】ElasticSearch之定时删除数据

    有的时候我们在使用ES时,由于资源有限或业务需求,我们只想保存最近一段时间的数据,所以有如下脚本可以定时删除数据 delete_es_by_day.sh #!/bin/sh # example: in ...

  5. 【BZOJ】4361: isn

    题解 可以想一下保留一个长度为k的不降序列方案数是\(f[k] (n - k)!\) \(f[k]\)是有多少个长度为k的不降序列 我们去掉不合法的,一定是前一次操作的时候有一个长度为\(k + 1\ ...

  6. Codeforces 466E Information Graph

    Information Graph 把询问离线之后就能随便搞了, 去check一下是不是祖先, 可以用倍增也能用dfs序. #include<bits/stdc++.h> #define ...

  7. 针对LDAP安装web接口,进行管理

    1. 通过SSH连接LDAP服务器 2. 安装phpLDAPadmin运行以下命令. $ sudo apt-get install phpldapadmin 3. 配置phpLDAPadmin. $ ...

  8. Flyweight模式_Java中23种设计模式

    —————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— 享元模式: Flyweight模式的有效性很大程度上取决于如何使用它以及在何处使用 ...

  9. 恢复spark挂掉的节点

    背景: 某个Worker节点挂掉了,我们需要将Worker进行启动恢复,同时可以恢复streaming中的executor 解决办法: 其实很简单,去worker节点中执行sbin/start-sla ...

  10. 【noip模拟赛3】拣钱

    描述 最近,Henry由于失恋(被某大牛甩掉!)心情很是郁闷.所以,他去了大牛家,寻求Michael大牛的帮助,让他尽快从失恋的痛苦中解脱出来.Michael大牛知道Henry是很爱钱的,所以他是费尽 ...