A Short Note – An Overview Of Android Foreground Service Launch Restrictions

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (An Overview Of Android Foreground Service Launch Restrictions).

In this note series, We will understand about Android Foreground Service Launch Restrictions in Android 12.

Apps that target Android 12 can no longer start foreground services while running in the background, except for a few special cases. If an app tries to start a foreground service while the app is running in the background, and the foreground service doesn’t satisfy one of the exceptional cases, the system throws a ForegroundServiceStartNotAllowedException.

So Let’s begin.

Recommended alternative to foreground services

If our app is affected by this change, we can migrate to using WorkManager. WorkManager is the recommended solution for starting higher-priority background tasks.

Starting in WorkManager 2.7.0, our app can call setExpedited() to declare that a Worker should use an expedited job. This new API uses expedited jobs when running on Android 12, and the API uses foreground services on prior versions of Android to provide backward compatibility.

The following code snippet shows an example of how to use the setExpedited() method:

OneTimeWorkRequestBuilder<T>().apply {
    setInputData(inputData)
    setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
}.build()

Because the CoroutineWorker.setForeground() and ListenableWorker.setForegroundAsync() methods are backed by foreground services, they’re subject to the same foreground service launch restrictions and exemptions. We can use the API opportunistically, but be prepared to handle an exception if the system disallows our app from starting a foreground service. For a more consistent experience, use setExpedited().

Cases where foreground service launches from the background are allowed

In the following situations, our app can start foreground services even while our app is running in the background:

  • Our app transitions from a user-visible state, such as an activity.
  • App can start an activity from the background, except for the case where the app has an activity in the back stack of an existing task.
  • Our app receives a high-priority message using Firebase Cloud Messaging.
  • The user performs an action on a UI element related to our app. For example, they might interact with a bubble, notification, widget, or activity.
  • Our app receives an event that’s related to geofencing or activity recognition transition.
  • After the device reboots and receives the ACTION_BOOT_COMPLETEDACTION_LOCKED_BOOT_COMPLETED, or ACTION_MY_PACKAGE_REPLACED intent action in a broadcast receiver.
  • Our app receives the ACTION_TIMEZONE_CHANGEDACTION_TIME_CHANGED, or ACTION_LOCALE_CHANGED intent action in a broadcast receiver.
  • App receives a Bluetooth broadcast that requires the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permissions.
  • Apps with certain system roles or permission, such as device owners and profile owners.
  • Our app uses the Companion Device Manager. To let the system wake our app whenever a companion device is nearby, implement the Companion Device Service in Android 12.
  • The system restarts a “sticky” foreground service. To make a foreground service sticky, return either START_STICKY or START_REDELIVER_INTENT from onStartCommand().
  • The user turns off battery optimizations for your app. We can help users find this option by sending them to our app’s App info page in system settings. To do so, invoke an intent that contains the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent action.

Conclusion

In this note series, we understood about Android Foreground services launch restrictions in Android 12. We discussed about cases where foreground service launches from the background are allowed.

Thanks for reading! I hope you enjoyed and learned about Foreground service launch restrictions in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Loading

Summary
A Short Note - An Overview Of Android Foreground Service Launch Restrictions
Article Name
A Short Note - An Overview Of Android Foreground Service Launch Restrictions
Description
This note series explains about Android Foreground Services Launch Restrictions with special allowed cases where foreground service launches.
Author

3 thoughts on “A Short Note – An Overview Of Android Foreground Service Launch Restrictions”

  1. Have you ever thought about adding a little bit more than just your articles?

    I mean, what you say is important and all. Nevertheless imagine if you added some great photos or video clips
    to give your posts more, “pop”! Your content is excellent
    but with pics and clips, this website could undeniably be one of the best in its field.
    Amazing blog!

    Reply

Leave a Comment