Android - sendOrderedBroadcast help

I am trying to use sendOrderedBroadcast in my android application.

I want to be able to send an Intent from one of my applications to another, and then I want to get the data back from the application that receives the Intent, in which case the boolean value is true or false.

Here is the current 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 right way to achieve what you want?

If so, what am I using as resultReceiver * parameter? (Third parameter)

And then how do I get the data back from the broadcast?

I made a quick google and do not come up with any examples, any help or examples that were highly appreciated.

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

So, according to the doc, I have to return 100 back to the DomainBroadcast receiver, but it always returns as 0.

Can anyone understand why?

** resultReceiver - your own BroadcastReceiver for processing as the final broadcast receiver. *

+7
android android-intent broadcast broadcastreceiver
source share
1 answer

You need to get additional Bundle data and add your data to it.

Try something like this:

 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 result:

 $ 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 
+13
source share

All Articles