在Android中使用ResultReceiver

时间:2010-12-22 15:57:28

标签: android service callback android-activity

从根本上说,我想从IntentService建立一个Activity的回调。我的问题与这里回答的问题非常相似:

Restful API service

但是,在答案代码中,活动代码被视为实现ResultReceiver。除非我遗漏了什么,否则ResultReceiver实际上是一个类,所以它无法执行此实现。

基本上,我问的是将ResultReceiver连接到该服务的正确方法是什么。我对Handler和ResultReceiver概念感到困惑。任何工作示例代码都将受到赞赏。

5 个答案:

答案 0 :(得分:113)

  1. 您需要从中扩展自定义resultreceiver类 ResultReceiver

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

  3. 将自定义resultreceiver对象传递给intentService并在其中 intentservice只是获取接收器对象并调用 receiver.send()函数将任何内容发送到调用活动中 捆绑对象。

    这里是customResultReceiver类:

     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);
            }
        }
    
    }
    
  4. 在您的活动中实现Myresultreceiver.receiver接口,创建一个类变量

    Public MyResultReceiver mReceiver;
    

    在onCreate中初始化此变量:

    mReceiver = new MyResultReceiver(new Handler());
    
    mReceiver.setReceiver(this);
    

    通过以下方式将此mReceiver传递给intentService:

    intent.putExtra("receiverTag", mReceiver);
    

    并在IntentService中获取,如:

    ResultReceiver rec = intent.getParcelableExtra("receiverTag");
    

    并使用rec as:

    向活动发送任何内容
    Bundle b=new Bundle();
    rec.send(0, b);
    

    这将在活动的onReceiveResult中收到。您可以在以下网址查看完整代码:IntentService: Providing data back to Activity

    编辑:您应该在onResume中调用setReceiver(this),在onPause()中调用setReceiver(null)以避免泄漏。

答案 1 :(得分:22)

通过子类覆盖方法。它不一定是一个接口来做到这一点。

例如:

intent.putExtra(StockService.REQUEST_RECEIVER_EXTRA, new ResultReceiver(null) {
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        if (resultCode == StockService.RESULT_ID_QUOTE) {
            ...
        }
    }
});

答案 2 :(得分:10)

我创建了一个演示如何使用ResultReceiver

的简单示例

<强> MainActivity

public class MainActivity extends AppCompatActivity {

    private final static String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent serviceIntent = new Intent(this, MyService.class);
        serviceIntent.putExtra("logName", "MAIN_ACTIVITY");
        serviceIntent.putExtra(MyService.BUNDLED_LISTENER, new ResultReceiver(new Handler()) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);

                if (resultCode == Activity.RESULT_OK) {
                    String val = resultData.getString("value");
                    Log.i(TAG, "++++++++++++RESULT_OK+++++++++++ [" + val + "]");
                } else {
                    Log.i(TAG, "+++++++++++++RESULT_NOT_OK++++++++++++");
                }
            }
        });
        startService(serviceIntent);
    }
}

<强>为MyService

public class MyService extends Service {

    private final static String TAG = MyService.class.getSimpleName();
    public final static String BUNDLED_LISTENER = "listener";

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String logName = intent.getStringExtra("logName");
        ResultReceiver receiver = intent.getParcelableExtra(MyService.BUNDLED_LISTENER);

        Bundle bundle = new Bundle();
        bundle.putString("value", "30");
        receiver.send(Activity.RESULT_OK, bundle);
        return Service.START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

答案 3 :(得分:-1)

我现在会用EventBus来做这件事

https://github.com/google/guava/wiki/EventBusExplained

答案 4 :(得分:-4)

在android中使用Resulteceiver

  1. 创建SomeResultReceiver从resultReceiver扩展

  2. 使用on方法创建接口someReceiver,例如onReceivResult(int resultCode,Bundle resultData);

  3. 3.在someResultreceiver中使用someReceiver

    1. create someService扩展了IntentService并使用someresultReceiver.send()方法将服务的结果发送到someOne类(例如:MyActivity)

    2. 在活动

    3. 上实施somereceiver

      6.在MyActivity类和setreceiver

      中实例化someResultReceiver
      1. 使用Intent的startService和putExtra someResultreceiver instanse
      2. 了解更多详情ResultReceiver Class请参阅enter link description here