Android – sendOrderedBroadcast的帮助

我想在我的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 *参数? (第三个参数)

那么如何从广播中接收数据呢?

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

更新代码:


 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作为广播的最终接收者。*

Solutions Collecting From Web of "Android – sendOrderedBroadcast的帮助"

您需要获取额外的数据结果包 ,并将数据添加到其中。

尝试这样的事情:

 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