Android - sendOrderedBroadcast帮助

时间:2010-05-31 12:06:13

标签: android android-intent broadcast broadcastreceiver

我正在尝试在我的Android应用中使用sendOrderedBroadcast

我希望能够将Intent从我的一个应用程序发送到另一个应用程序,然后我想从收到Intent的应用程序中获取数据,在这种情况下为布尔值true或false。

以下是当前代码:

    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);

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

如果是这样,我将什么用作resultReceiver *参数? (第3个参数)

然后我如何从广播中收回数据?

我做了一个快速的谷歌,没有提出任何例子,任何帮助或例子非常感谢。

更新后的代码:


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);

          }

所以根据Doc的说法,我应该在我的DomainBroadcast接收器中收到100,但它总是回到0。

谁能明白为什么?

** resultReceiver - 您自己的BroadcastReceiver将其视为广播的最终接收者。*

1 个答案:

答案 0 :(得分:13)

您需要get the extra data results Bundle并将数据添加到其中。

尝试这样的事情:

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);
    }
}

产生所需的输出:

$ 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