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. day16作业

    一.填空题 1.Integer Character 2.String s = "123";Integer i = new Integer(s);System.out.println ...

  2. Windows caffe VGG人脸识别

    caffe自带的例子有mnist和cifar10,cifar10和mnist的运行方式类型,下好图片数据文件后,训练例子中的模型,然后测试模型,也可以自己用图片进行预测分类(自己图片最好是cifar1 ...

  3. java collection和Iterator

    Collection是描述所有 序列容器的共性的根接口,它可以被认为是一个"附属接口",即因为要表示其他若干个接口的共性而出现的接口,另外,java.uitl.AbstaractC ...

  4. Intellij IDEA调试功能总结

    public class Demo { public static void f1() { System.out.println("one"); System.out.printl ...

  5. ElastAlert告警

    ElastAlert告警 https://blog.csdn.net/qq_38369069/article/details/80842432

  6. json多态序列化

    https://blog.csdn.net/java_huashan/article/details/46428971 https://blog.csdn.net/bruce128/article/d ...

  7. tispark部署步骤

    正常在我们的环境使用tidb集群,都默认没有spark集群的,但之前部署tidb的时候,spark默认已经和系统编译,下面我们的工作就是搭建spark集群和tikv融合 官方是要我们下载tispark ...

  8. Mysql介绍,与将脚本导入新数据库

    一:介绍 1.介绍 Mysql是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系 ...

  9. php 会话控制(理解会话控制的概念)

    理解一个概念就需要理解他的背景及产生的原因,这里引入web环境及其http协议. 会话控制产生的背景: http协议是web服务器与客户端相互通信的协议,它是一种无状态协议,所谓无状态,指的是不会维护 ...

  10. git入门教程,主要命令详解。

    准备工作 git clone url / ssh  ----------------------------------------------------------------------从git ...