LocalBroadcastManager order of execution

487 views Asked by At

I am trying to understand how BroadcastReceivers work on Android. My question is, will there be a delay between the time I call sendBroadcast on a LocalBroadcastManager to the time it is received in my BroadcastReceiver? Will the call be synchronous?

For example, when invoking myFunction, will the output be 21 or 12??

myFunction {
  sendBroadcast;
  print "1";
}

myReceiver {
  print "2";
}

what if the function running is changed to

myFunction {
  sendBroadcast1;
  print "1";
  sendBroadcast2;
  callALotOfOtherFunctions;
}

myReceiver1 {
  print "2";
}

myReceiver2 {
  print "3";
}

will all the other functions called from myFunction be called before the receivers?

2

There are 2 answers

4
android developer On BEST ANSWER

intents are being sent one after another, just like any event on the message queue.

there is no delay, only waiting in line for your event/intent to be taken care of , since there might be other events/intent to be handled first.

they are all being called on the UI thread, which loops over all of the events (and intents), therefore it's called the main looper thread.

2
TootsieRockNRoll On

I believe BroadcastReceiver is working asynchronously, so (and I'm not sure) yes your functions could run before completing your receivers tasks,

you should try this and see results for your self

myFunction {
  sendBroadcast1;
  print "1";
  sendBroadcast2;
  callALotOfOtherFunctions;
}

myReceiver1 {
  print("rec1 begins")
  sleep(1000)
  print("rec1 ends")
}

myReceiver2 {
  print("rec2 begins")
  sleep(1000)
  print("rec2 ends")
}

see if your functions are called before the print