Android的 - sendOrderedBroadcast帮助Android、sendOrderedBroadcast

由网友(心痛得淋漓畅快。)分享简介:我试图用一个sendOrderedBroadcast在我的Andr​​oid应用程序。I am trying to use a sendOrderedBroadcast in my Android app.我希望能够从我的应用程序之一发送意图到另一个,然后我想找回数据从临危的意图,在这种情况下,布尔真或假的应用程序...

我试图用一个sendOrderedBroadcast在我的Andr​​oid应用程序。

I am trying to use a sendOrderedBroadcast in my Android app.

我希望能够从我的应用程序之一发送意图到另一个,然后我想找回数据从临危的意图,在这种情况下,布尔真或假的应用程序。

I want to be able to send the Intent from one of my applications to another and I then want to get data back from the Application that recieves the Intent, in this case a boolean true or false.

下面是当前code:

    Intent i = new Intent();
    i.setAction(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT);
    i.putExtra("com.testnetworks.QCLEVEL", aProposedTheoreticalQoSLevel);
    sendOrderedBroadcast(i, null, null, null, Activity.RESULT_OK, null, null);

这是实现我想要的正确方法是什么?

Is this the correct way to achieve what I want?

如果让我为resultReceiver *参数用什么? (第三个参数)

If so what do I use as the resultReceiver* parameter? (3rd parameter)

再怎么我收到的数据从广播回来?

And then how to I recieve data back from the Broadcast?

我已经做了快速谷歌并没有拿出任何的例子,任何帮助或示例大大AP preciated。

I have done a quick google and not come up with any examples, any help or examples greatly appreciated.

更新时间是code:

sendOrderedBroadcast(i, null, domainBroadcast, null, Activity.RESULT_OK, null, null);

class DomainBroadcast extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();

        if(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT.equals(action)){
            Log.d("BROADCAST", "Returning broadcast");

            Bundle b = intent.getExtras(); 
            Log.d("BROADCAST", "Returning broadcast " + 
                    b.getInt("com.testnetworks.INT_TEST"));
        }      
    }

    @Override
    public void onReceive(Context context, Intent intent) {
            String action = intent.getAction(); 

            if(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT.equals(action)){

                Bundle b = intent.getExtras();
                int testQCLevel = b.getInt("com.testnetworks.QCLEVEL");
                switch(testQCLevel){
                case 1:
                    Log.d("QCLevel ", "QCLevel = UNAVAILABLE");
                    break;
                case 2:
                    Log.d("QCLevel ", "QCLevel = BELOWUSABILITY");
                    break;
                case 3:
                    Log.d("QCLevel ", "QCLevel = VOICE");
                    break;
                }

                intent.putExtra("com.testnetworks.INT_TEST", 100);

          }

所以,根据美国商务部的我应该接受她100早在我DomainBroadcast reciever但它总是回来为0。

So according to the Doc's I should recieve 100 back in my DomainBroadcast reciever but it always comes back as 0.

任何人都可以看到,为什么?

Can anyone see why?

*的 resultReceiver - 你自己的BroadcastReceiver治疗作为广播的最终接收者的

推荐答案

您需要get额外的数据结果捆绑您的信息添加到它。

You need to get the extra data results Bundle and add your data to it.

尝试是这样的:

public class DemoOrderedBroadcast extends Activity {
    private static String SOMETHING_HAPPENED = "com.example.somethinghappened";
    private static String EXTRA_INTEGER = "extra integer";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        IntentFilter filter = new IntentFilter(SOMETHING_HAPPENED);

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle results = getResultExtras(true);
                results.putInt(EXTRA_INTEGER, 100);
                Log.d("DemoOrderedBroadcast",
                        "In Initial Receiver: Put 'extra integer' = 100");
            }
        }, filter);

        Intent intent = new Intent(SOMETHING_HAPPENED);
        sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle results = getResultExtras(true);
                Log.d("DemoOrderedBroadcast",
                        "In Result Receiver: Got 'extra integer' = "
                                + results.getInt(EXTRA_INTEGER, -1));
            }
        }, null, Activity.RESULT_OK, null, null);
    }
}

其中产生所需的输出:

Which produces the desired output:

$ adb -e shell am start -n com.example.DemoOrderedBroadcast/.DemoOrderedBroadcast
Starting: Intent { cmp=com.example.DemoOrderedBroadcast/.DemoOrderedBroadcast }
$ adb -e shell logcat | grep D/DemoOrderedBroadcast
D/DemoOrderedBroadcast( 1343): In Initial Receiver: Put 'extra integer' = 100
D/DemoOrderedBroadcast( 1343): In Result Receiver: Got 'extra integer' = 100
阅读全文

相关推荐

最新文章