Search code examples
androidforeground-service

Android 12 able to start foreground activities in the background with ConnectivityManager


I have an Intent that was started using startForegroundService(intent)

What triggers it is

connectivityManager.registerDefaultNetworkCallback(object : NetworkCallback() {
      override fun onAvailable(network: Network) {
        startForegroundService(intent)
      }
    })

I thought Android12 doesn't let you start foreground services from the background, and this doesn't seem to fall in any exemption cases. How come the code ran without any issue?


Solution

  • Are you sure that your app's targetSdk is >= 31? It sounds like it's probably targeting a lower level, and in that case, the background restrictions don't apply.

    Here's the actual page from Google, and a direct quote:

    Apps that target Android 12 or higher can't start foreground services while running in the background, except for a few special cases. If an app attempts to start a foreground service while running in the background, an exception occurs (except for the few special cases)