Android SDK introduce BroadcastReceiver.onReceive method. Is it called only from the…
- User
Android SDK introduce BroadcastReceiver.onReceive method. Is it called only from the main thread?
- Assistant
No, the
onReceivemethod of aBroadcastReceiverin the Android SDK is not guaranteed to be called on the main thread. The Android system may call theonReceivemethod on a separate worker thread to allow the receiver to process the broadcast intent in the background and avoid blocking the main thread. - User
Is it safe to call "ContextCompat.startForegroundService" from a worker thread?
- Assistant
No, it's not safe to call
ContextCompat.startForegroundServicefrom a worker thread.ContextCompat.startForegroundServicemust be called from the main thread, as it involves interaction with the system's UI, which must always be done on the main thread. Additionally, starting a foreground service requires showing a notification to the user, which also requires the main thread.If you need to start a foreground service from a worker thread, you should use a
HandlerorHandlerThreadto post a message to the main thread that starts the service. - User
Which methods of Service interface would be called until ContextCompat.startForegroundService() returns?
- Assistant
The following methods of the
Serviceinterface may be called untilContextCompat.startForegroundService()returns:onCreate(): This method is called when the service is first created.onStartCommand(): This method is called when the service is started by callingstartService()orstartForegroundService().onBind(): This method is called when a client binds to the service by callingbindService(). Note that this method is not called if the service was started withstartService()orstartForegroundService().
Once
ContextCompat.startForegroundService()returns, the Android system will continue to callonStartCommand()as needed to process command requests for the service.It's important to note that the exact order in which these methods are called may vary depending on the specific requirements of your app and the Android system.
- User
Does the OS count the accumulated duration of those methods' processing time to raise ForegroundServiceDidNotStartInTimeException?
- Assistant
Yes, the Android operating system does count the accumulated duration of the
onCreate(),onStartCommand(), andonBind()methods' processing time when determining whether to raise aForegroundServiceDidNotStartInTimeException.Starting with Android 9.0 (Pie), the Android system imposes a time limit of 5 seconds for a foreground service to become active, which includes the time taken by these methods to execute. If the service does not call
startForeground()within this time limit, the system will raise theForegroundServiceDidNotStartInTimeExceptionto indicate that the service failed to start in a timely manner.It's important to ensure that the processing time of these methods is kept to a minimum to avoid running afoul of the time limit imposed by the system. If you need to perform time-consuming operations in a foreground service, it's best to use a worker thread to perform those operations in the background and return control to the main thread as quickly as possible.