0

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?

Jeshurun
  • 22,940
  • 6
  • 79
  • 92
BarbM
  • 11
  • 2
  • Why don't you try it and see. – Kuffs Dec 20 '13 at 22:05
  • if I try it I will get an answer for what I've tried. Im looking for an explanation of how it works and not what will be the outcome in some specific ooccasions – BarbM Dec 20 '13 at 22:07

2 Answers2

1

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.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • In an ideal environment, where there are no other intents in the queue. What has priority? Handling the events/intents or proceeding with the execution of the code?? The main looper thread sounds as a worker thread to me, but is this the case? – BarbM Dec 20 '13 at 22:23
  • it's like a queue. you add more items to it, and only when you are done with the current function call ("myFunction" in your case), the intents that were gathered will be handle, one after another, meaning the order will be "myFunction" , whoever listens to "sendBroadcast1" , and then whoever listens to "sendBroadcast2". this is exactly the same as any other event . for example onClickEvent - if you've debugged the app and caused it to stop on a certain function, yet clicked multiple times on a button, the events are gathered and when you release the debugging, all will be handled one by one. – android developer Dec 20 '13 at 22:28
  • hmm ok, so you are basically saying that in the second example, it will print 1 and after all the other executions from myFunction are finished, it will print 23. I will test the scenario and hopefully I will get more feedback from more people, and if this is the case, I will upvote your answer. – BarbM Dec 20 '13 at 22:51
  • it you have a function that prints 1 , then sends broadcast1 and then sends broadcast2 , what will happen is that you finish the function as normal, then someone (if exists) gets the first broadcast and handles it , and then someone (if exists) gets the second broadcast and handles it . so if i understand your example correctly, it wil print 1, then 2, then 3. – android developer Dec 20 '13 at 23:59
0

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

TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50
  • I dont think it is async as it is specified in the documentation that they are called in the UI thread except if a handler is used..and that's what confuses me, if they are not async, how they are handled.. – BarbM Dec 20 '13 at 22:17
  • hmmm, so may be you could try an example, and keep us posted – TootsieRockNRoll Dec 20 '13 at 22:19