Reputation: 2814
Is it better (for performance) to send a broadcast (ACTION_APPWIDGET_UPDATE in my case), in a separate thread than the UI thread (a runnable)? Or is it acceptable practice to do so on the UI thread?
Upvotes: 4
Views: 2515
Reputation: 1861
In developer console in some of my apps I check ANR (UI thread blocked) in some devices (less than 0.5%) when call "sendBroadcast" many times. To solve I call sendBroadcast in backround thread.
So it seems that even if it is asynchronous if you call it many times it can be blocked.
Upvotes: 1
Reputation: 1135
As you know BroadcastReceiver.onReceive always run in the UI thread. You register the receiver dynamically, you can specify that another thread handles the onReceive(). This is done through the Handler parameter of registerReceiver().
So, if preferably you should use through UI.
Upvotes: 0
Reputation: 64933
Broadcasts are always sent asynchronously, you do not need to run it in a separate thread to avoid blocking the UI thread. sendBroadcast() is already non blocking. From sendBroadcast's documentation:
public abstract void sendBroadcast (Intent intent)
Broadcast the given intent to all interested BroadcastReceivers, allowing an optional required permission to be enforced. This call is asynchronous; it returns immediately, and you will continue executing while the receivers are run ...
Upvotes: 1
Reputation: 50036
You can read in documentation for sendBroadcast:
This call is asynchronous; it returns immediately, and you will continue executing while the receivers are run.
so it is perfectly safe to call it on UI thread
Upvotes: 6
Reputation: 3831
Sending broadcast is not a time taking(long running) process. So you can send the broadcast from the main thread(UI thread) also.
Upvotes: 1