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 ...
随机推荐
- 如何使用windows的计划任务?计划任务?
我们经常有一些程序想要过了几小时来运行:比如定时关机 或者说希望能够每天的几点执行一个什么程序: 这些所有的操作都需要用到windows的任务计划:或者叫计划任务:反正都一样 下面小编将指导大家创建一 ...
- leetcode 题集
775. Global and Local Inversions 统计相邻元素组成的逆序对(local inversion)和全局逆序对的数量(global inversion) 思路:local i ...
- web html调用百度地图
如果想在自己的网页上面加入百度地图的话,可以用百度地图的api.具体使用方法如下: 第一步:进入百度创建地图的网站http://api.map.baidu.com/lbsapi/creatmap/,搜 ...
- .NetCore利用Swagger生成 XML文档需要注意生成路径的地址
发布的时候如果用 release dotnet publish --configuration release dotnet publish 默认都是debug 会出现 XML丢失问题,其实可以看下工 ...
- spark优化:spark.serializer修改序列化方式
进行节点的数据传递,或者保存数据时都会进行序列化.spark默认的是org.apache.spark.serializer.JavaSerializer.而我们要修改成org.apache.spark ...
- Db2与Oracle的区别
这个部分是自己随便整理下. 在工作上需要,公司需要DB2兼容Oracle. 不懂DB2与Oracle的细节,这里努力整理,以后精通了再回来重新修改. https://www.2cto.com/data ...
- linux cudnn安装
cudnn-8.0-linux-x64-v5.1链接:http://pan.baidu.com/s/1c1JuMty 密码:v0g9 #以CuDNN的v5.1版本,Cuda 8.0为例 sudo ta ...
- 利用Httponly提升web应用程序安全性(转)
原文:http://kb.cnblogs.com/page/115136/ 随着www服务的兴起,越来越多的应用程序转向了B/S结构,这样只需要一个浏览器就可以访问各种各样的web服务,但是这样也越来 ...
- Android-Activity的切换效果
Android-Activity的切换效果 Android-Activity的切换效果 Activity有一个默认的切换效果,但是有时候单一的切换效果未免单调,Activity的切换效果也是我们可以自 ...
- 2977 二叉堆练习1 codevs
题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 Input Description 二叉树的节点数N和N个节点(按层输入) 输出描述 Output Descr ...