IntentService 与ResultReceiver
来源:互联网 发布:ios7cydia软件源 编辑:程序博客网 时间:2024/06/03 19:18
(转载)http://www.cnblogs.com/wanqieddy/p/3860388.html
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即有service属性,又有handlerthread属性,
(1)能否像service那样使用serviceconnection通信。
(2)能否使用Handler和HandlerThread来通信。
- IntentService 与ResultReceiver
- IntentService 与ResultReceiver
- IntentService与ResultReceiver的应用
- 用IntentService 与ResultReceiver连接web service
- Activity与Service通信之ResultReceiver
- 线程相关——HandlerThread、IntentService、ResultReceiver:结果接收者、AsyncTask:异步任务、Android中处理线程间通信的方式
- android ResultReceiver
- Android Service 与 IntentService
- Service与IntentService
- Service 与 IntentService
- Service 与 IntentService
- Service与IntentService
- Service与IntentService
- IntentService 使用与原理
- IntentService解析与使用
- Service与IntentService的比较
- Service与IntentService的比较
- HandlerThread与IntentService原理解析
- 【BZOJ1079】[SCOI2008]着色方案【计数DP】【奇怪的姿势】
- 本地推送
- 《Python数据分析基础教程:Numpy学习指南》- 速记 - 第七章
- 欢迎使用CSDN-markdown编辑器
- Codeforces 658C Bear and Forgotten Tree 3【构造】
- IntentService 与ResultReceiver
- ArduPilotMega (APM) 是市面上最强大的基于惯性导航的开源自驾仪
- Animation 动画介绍和实现
- 录像、录音和拍照
- USING DEEP LEARNING FOR ANOMALY DETECTION IN RADIOLOGICAL IMAGES
- TCP/IP协议
- ViewPager滑动速度调整的方法
- 机器学习基础(二十九)—— 径向基核函数(RBF)
- nginx