为什么BroadcastReceiver的调用的setResult? [英] Why call setResult in BroadcastReceiver?

查看:646
本文介绍了为什么BroadcastReceiver的调用的setResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从谷歌的C2DM样品code,当通知中的BroadcastReceiver recived他们称之为:

的setResult(Activity.RESULT_OK,空/ *数据* /空/ *额外* /);

我不知道是什么的setResult做。这是他们在Android的文档说:

  

更改所有从这个节目返回的结果的数据;只要   适用于通过Context.sendOrderedBroadcast发送广播。所有   当前结果数据被替换为给定的这种方法的值

有人可以解释他们的意思是,为什么我需要调用它?

完成code:

 公共类C2DMBaseReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        C2DMReceiver.runIntentInService();
        的setResult(Activity.RESULT_OK,空/ *数据* /空/ *额外* /);
    }
}
 

解决方案

在broadcastreceivers的的setResult方法,它比什么都用于跟踪多了,如果你熟悉了活动类的setResult方法,你可以把这个方法广播以同样的方式,但在活动的情况下,得到一个回调方法,如onActivityResult代替,广播的setResult方法用于跟踪广播的结果按照一定的顺序,这就是为什么文件说: 只能通过Context.sendOrderedBroadcast发送广播。目前所有的结果数据被替换为给这种方法的价值。至极意味着你可以使用的方法的getResult code,getResultData或getResultExtras知道事情的onReceive方法在所有注册来处理你的广播不同broadcastreceivers执行过程中怎么去了。所以,你可以知道在previous广播接收器的code目前执行沿着所有的接收器执行的前一个叫的结果。

报告说,只有Context.sendOrderedBroadcast。因为定期调用sendBroadcast方法可能不会等待1接收机启动另一个线程来执行code在其他接收器监听同样的意图,以及前完成它的执行。

in the C2DM sample code from google, when a notification recived in BroadcastReceiver they call :

setResult(Activity.RESULT_OK, null /* data */, null /* extra */);

I didnt know what the setResult do. this is what they say in Android docs :

Change all of the result data returned from this broadcasts; only works with broadcasts sent through Context.sendOrderedBroadcast. All current result data is replaced by the value given to this method.

Can somebody explain what they mean and why i need to call it?

Complete code :

public class C2DMBaseReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        C2DMReceiver.runIntentInService();
        setResult(Activity.RESULT_OK, null /* data */, null /* extra */);
    }
}

解决方案

The setResult method in broadcastreceivers, its more than anything for tracking purposes, if you are familiarized with the setResult method of the activity class, you can think of this method in broadcast in the same way, but in stead of getting a callback method like "onActivityResult" in the case of activities, broadcast setResult method is used to keep track of the results of the broadcasts in a certain order, that's why the documentation says: "only works with broadcasts sent through Context.sendOrderedBroadcast. All current result data is replaced by the value given to this method." wich means that you can make use of the methods getResultCode, getResultData or getResultExtras to know how things went during the execution of the onReceive method in all the different broadcastreceivers registered to handle your broadcast. So you can know the result of the code execution in the previous broadcast receiver called before the one currently executed along all the receivers.

It says only "Context.sendOrderedBroadcast." because a regular call to sendBroadcast method might not wait for 1 receiver to complete its execution before starting another thread to execute code in other receiver listening the same intent as well.

这篇关于为什么BroadcastReceiver的调用的setResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆