Android ResultReceiver 汇总
来源:互联网 发布:java bug管理系统 编辑:程序博客网 时间:2024/05/21 15:40
收集的相关信息:
<1> From: http://www.eoeandroid.com/thread-325401-1-1.html
发现网络上对ResultReceiver的资料很少,刚好项目中用到,就花点时间整理下,提大家参考。
首先看下官网对 ResultReceiver的解释简单翻译下:
举个例子,假如我们操作完一系列UI操作之后,需要写日志。这时候会开个子线程来完成这个动作
可能根据不同的UI操作,写入的日志会有所不同。下面我们通过IntentService来简单实现下这个功能(当然你还有其他方式)
首先我们有用两个按钮来模拟两个进行的请求
在主线程中,启动一个IntentService
接着看下MyIntentService
最后是MyResultReceiver
到此,介绍就算结束了。
附上ResultReceiver的中文API
http://www.apihome.cn/api/android/ResultReceiver.html
<2> From: http://stackoverflow.com/questions/4510974/using-resultreceiver-in-android
You need to make custom resultreceiver class extended from ResultReceiver
then implements the resultreceiver interface in your activity
Pass custom resultreceiver object to intentService and in intentservice just fetch the receiver object and call receiver.send() function to send anything to the calling activity in Bundle object.
here is customResultReceiver class :
public class MyResultReceiver extends ResultReceiver { private Receiver mReceiver; public MyResultReceiver(Handler handler) { super(handler); // TODO Auto-generated constructor stub } public interface Receiver { public void onReceiveResult(int resultCode, Bundle resultData); } public void setReceiver(Receiver receiver) { mReceiver = receiver; } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (mReceiver != null) { mReceiver.onReceiveResult(resultCode, resultData); } }}
implements the Myresultreceiver.receiver interface in you activity, create a class variable
Public MyResultReceiver mReceiver;
initialize this variable in onCreate:
mReceiver = new MyResultReceiver(new Handler());mReceiver.setReceiver(this);
Pass this mReceiver to the intentService via:
intent.putExtra("receiverTag", mReceiver);
and fetch in IntentService like:
ResultReceiver rec = intent.getParcelableExtra("receiverTag");
and send anything to activity using rec as:
Bundle b=new Bundle();rec.send(0, b);
this will be received in onReceiveResult of the activity. You can view complete code at:IntentService: Providing data back to Activity
Edit: You should call setReceiver(this) in onResume and setReceiver(null) in onPause() to avoid leaks.
<3>http://stackoverflow.com/questions/13178075/broadcast-receiver-and-resultreceiver-in-android
What is difference between BroadcastReceiver and ResultReceiver in android?
Result Receiver:
Generic interface for receiving a callback result from someone.
Broadcast Receiver:
Base class for code that will receive intents sent by sendBroadcast().
EDIT:
Background: All networking operations/long running operations should take place away from the main thread. Two ways to do this :
- Async task - For Simple networking like say retreive an image/ do db processing
- Service - For Complex long running background process
If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create an Async Thread. But if you want the process to continue even after the user exits the app (say a download) then use a service
Lets say you pick 2. Now
- You activity sends a web request to your service
- Your service executes that using say DefaultHttpClient
It sends back data to your activity.
The third step of receiving data here can be done in two ways
1.) Broadcast receiver: Multiple receivers can receive your data. Used if you want to send data/notifications across applications(say you are also interacting with fb and twitter, multiple receivers for your web broadcast), whenever you send broadcast its sent system wide.
2.) Result receiver: Your application is the only receiver of the data. It is an Interface you implement and pass it to the intentService through putExtra. IntentService will then fetch this object and call its receiver.send function to send anything (in bundle) to calling activity. Result receiver has preference over broadcast receivers if your all communication is internal to your application
EDIT: I should also mention this caution
Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.
- Android ResultReceiver 汇总
- Android ResultReceiver 汇总
- android ResultReceiver
- Android中ResultReceiver使用
- android ResultReceiver用法
- Android的有序无序BroadCast,ResultReceiver详解
- IntentService 与ResultReceiver
- IntentService 与ResultReceiver
- 线程相关——HandlerThread、IntentService、ResultReceiver:结果接收者、AsyncTask:异步任务、Android中处理线程间通信的方式
- IntentService与ResultReceiver的应用
- Android汇总
- Android汇总
- android汇总
- 用IntentService 与ResultReceiver连接web service
- Activity与Service通信之ResultReceiver
- Android Intent 用法汇总
- Android开发经验汇总
- Android 启动过程汇总
- 微信开发纪实之历史上的今天服务
- HDOJ:Problem 1005 汉诺塔V
- 在广袤的华北平原上
- UVa 11529 Strange Tax Calculation(极角排序)
- 2014的总结
- Android ResultReceiver 汇总
- 数组第二十课,数组的基本概念,内存分析
- Linux系统Squid代理添加用户、密码认证
- JBOSS系列(四)--集群--基础配置(1)
- 蓝桥杯 历届试题 剪格子 简单的DFS~~注意输入有陷阱~~
- grep/find用到的常用的例子
- 以Android中MediaServer启动来看Native层的Binder IPC通信机制
- asp.net MVC 使用signalR +bootstrap 实现 progressBar
- C++ 再谈string类型(1) 构造string对象的方法