Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When BroadcastReceiver runs, which thread is it running on, system wide or only your applications?

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.

like image 666
Andy Avatar asked Dec 27 '22 21:12

Andy


2 Answers

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

like image 132
JustinDanielson Avatar answered Apr 12 '23 13:04

JustinDanielson


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

like image 34
user1923551 Avatar answered Apr 12 '23 14:04

user1923551