Android ResultReceiver 汇总
来源:互联网 发布:excel横纵数据互换 编辑:程序博客网 时间:2024/06/06 00:14
(转载)http://blog.csdn.net/fyfcauc/article/details/43837261
收集的相关信息:
<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 启动过程汇总
- <context:component-scan>使用说明
- hibernate实现表联查并获取某个字段的最大值(随笔)
- 购物商城shopping连载(3)
- ios 属性中常用修饰词的总结
- 蓝桥杯省赛赛后感想
- Android ResultReceiver 汇总
- Android调用百度地图Web端接口,实现百度定位、导航
- 网络协议
- 魅族多机房部署方案
- 机器学习经典书籍--入门书-入门--深入--数学基础
- Quartz cron 表达式的格式
- 终于调通了从上到下,从驱动到应用,android
- AWK-详解著名的awk Oneliner,第三部分:选择性输出特定行
- android import library switch语句报错case expressions must be constant expressions