4

I have an Alarm go of using BroadcastReceiver, but I am a little confused on Threads. I would like to have it run on a separate thread so it doesn't cause unresponsiveness to anything else, but when looking on the Android Docs, I still don't really know if there is only one Main thread, or each application has its own Main thread.

For example, if my application isn't running, what would the point of running a separate thread in the BroadcastReceiver if each application runs its own Main thread, meaning I wouldn't be affecting the users' other processes. Since mine wasn't running, havent it do its thing wouldn't hurt. But in contrast, if there is one Main thread for all applications then I would need to move the actions to a separate thread. I hope I am not asking a stupid question. I just want to understand it thoroughly. Thanks in advance.

Andy
  • 10,553
  • 21
  • 75
  • 125

3 Answers3

8

After some browsing around the Android Developer page, I believe that BroadcastReceivers run on the Main IO thread.

BroadcastReceivers have the function goAsync that allows

the implementation to move work related to it over to another thread to avoid glitching the main UI thread due to disk IO.

Source

JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
  • Hmm, thanks for that link. Its very helpful to know. And I noticed that it said it ran on the Main IO thread, but does every app have a Main thread? – Andy Jul 12 '12 at 01:03
  • All apps share the Main thread. This is referred to as the IO thread, Main thread, or UI Thread. That's why some apps have that "UI Lag" issue. Processing on the UI thread will delay the processing of other UI events such as button presses and whatnot. http://appadvice.com/appnn/2011/12/android-lag-attributed-to-core-system-framework-here-to-stay – JustinDanielson Jul 12 '12 at 02:21
  • A google intern posted about it and it caused a firestorm, lots of people chimed in and gave their input. You'll learn a lot browsing around and reading links found here: https://plus.google.com/100838276097451809262/posts/VDkV9XaJRGS – JustinDanielson Jul 12 '12 at 02:22
  • I finally got a chance to read your comment, thank you very much for the links. Thats the answer I was looking. I figured everything shares a Main thread. – Andy Jul 13 '12 at 00:05
2

Basic rule of android is, all the components of android runs in Main thread (UI thread) by default.

Broadcast receivers are very light weight components of android, which has to do its functionality with in maximum of 10 seconds (as per android documentation).

Since you want to kick off an alarm from receiver, I don't think it is really going to have serious effect on your UI responsiveness for the user. So it is not really required to start a different thread just to kick off the alarm from your receiver.

Ref taken from developer android

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
user1923551
  • 4,684
  • 35
  • 29
0

A broadcast receiver is registered to the system that it is currently running on under the application that it is for. Specifically it does not halt anything on the main thread because it's done asynchronously. The message for this particular is broadcast and is then passed to whatever process is registered to listen to that broadcast via an IntentFilter (if it is protected it will require a permission or will not be available).

However; good practice is when you receive the broadcast, you start an AsyncTask to run whatever procedure you'd like to run.

You have nothing to worry about how the broadcast receiver works.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • So what you are saying is once its passed to the BroadcastReceiver it runs on my app and not whatever the user is doing? – Andy Jul 12 '12 at 01:02
  • That depends on what the broadcast receiver is doing... If it is running your app's Activity then yes it will interrupt the user. However; if its updating something in a database then no.. it will run silently. It just depends on the design. – JoxTraex Jul 12 '12 at 01:06
  • Ahh, I see. Well my BroadcastReciever just creates a notification with extras in the intent. Does that mean that it won't cause any UI issues, or at least not slow anything down? – Andy Jul 12 '12 at 01:16
  • No, it will not slow anything down as this will go into the background and update the status bar. – JoxTraex Jul 12 '12 at 06:58