本文介绍了在Android中使用ResultReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从根本上说,我想从IntentService建立一个回调的活动。我的问题是非常相似的一个在这里找到答案:

安卓:REST的API服务

然而,在答案code中,活动code为视为实施ResultReceiver。除非我失去了一些东西,ResultReceiver实际上是一类,所以它不能执行此实现。

所以基本上,我问什么是要连接一个ResultReceiver到该服务的正确方法。我感到困惑与处理程序和ResultReceiver概念,相对于这一点。任何工作示例code将AP preciated。

解决方案
  1. 您需要进行自定义resultreceiver类从延长ResultReceiver

  2. 然后实现resultreceiver接口您的活动

  3. 传递自定义resultreceiver对象intentService和intentservice只是获取接收对象,并调用receiver.send()函数来在发送任何给调用活动捆绑对象。

    下面是customResultReceiver类:

     公共类MyResultReceiver扩展ResultReceiver {
    
        私人接收mReceiver;
    
        公共MyResultReceiver(处理程序处理){
            超(处理);
            // TODO自动生成构造函数存根
        }
    
        公共接口接收器{
            公共无效onReceiveResult(INT结果code,捆绑resultData);
    
        }
    
        公共无效setReceiver(接收器接收器){
            mReceiver =接收器;
        }
    
        @覆盖
        保护无效onReceiveResult(INT结果code,捆绑resultData){
    
            如果(mReceiver!= NULL){
                mReceiver.onReceiveResult(结果code,resultData);
            }
        }
    
    }
     

实现Myresultreceiver.receiver接口,你的活动,创建一个类变量

 公共MyResultReceiver mReceiver;
 

在的onCreate初始化这个变量:

  mReceiver =新MyResultReceiver(新处理器());

mReceiver.setReceiver(本);
 

通过通过这个mReceiver到intentService:

  intent.putExtra(receiverTag,mReceiver);
 

和获取的IntentService这样的:

  ResultReceiver REC = intent.getParcelableExtra(receiverTag);
 

和使用REC为送什么活动:

 叠B =新包();
rec.send(0,B);
 

,这将在活动的onReceiveResult接收。您可以查看完整的code:IntentService:提供了数据备份到活动

编辑:你应该叫setReceiver(本)在onResume和setReceiver(空)的的onPause(),以避免泄漏

Fundamentally, I would like to establish a callback to an Activity from an IntentService. My question is very similar to the one answered here:

Android: restful API service

However, in the answer code, the activity code is seen as implementing a ResultReceiver. Unless I'm missing something, ResultReceiver is actually a class, so it cannot perform this implementation.

So essentially, I'm asking what would be the correct way to wire up a ResultReceiver to that service. I get confused with Handler and ResultReceiver concepts with respect to this. Any working sample code would be appreciated.

解决方案
  1. You need to make custom resultreceiver class extended fromResultReceiver

  2. then implements the resultreceiver interface in your activity

  3. Pass custom resultreceiver object to intentService and inintentservice just fetch the receiver object and callreceiver.send() function to send anything to the calling activity inBundle 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.

这篇关于在Android中使用ResultReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:36