title: Android 12 - Foreground service launch restrictions
body: <p>I'm developing an SDK that needs to <strong><a href="https://developer.android.com/reference/android/app/Service#startForeground(int,%20android.app.Notification)" rel="noreferrer">startForeground</a></strong> service from the background. Because it uses background location and Bluetooth-related works. If the application is killed, the monitoring is performing in the background. That's why I'm using the foreground service. There is a condition that starts the foreground service from the background.</p>
  <p>Currently, my SDK using <strong><a href="https://developer.android.com/reference/android/app/Service" rel="noreferrer">Service</a></strong> to handle this job. But <a href="https://developer.android.com/about/versions/12/foreground-services" rel="noreferrer">Android 12 on-words it doesn't support to start service from the background.</a></p>
  <p>I'm trying to start the service from the background the below exception throws.</p>
  <pre><code>ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false
  </code></pre>
  <p>How can I use <a href="https://developer.android.com/topic/libraries/architecture/workmanager" rel="noreferrer">WorkManager</a>  to fix this issue, all my handling is done by the Service class and how can I pass the Service object to <a href="https://developer.android.com/reference/androidx/work/Worker" rel="noreferrer">Worker</a> class and start this job inside the Worker class.</p>
  <p>Actually, my project is based on beacon technology.
  and the beacon signals are used to show different recommendations to the user.</p>
  <p>In my current implementation, if the application is killed by the user,
  and also accepts the foreground service, the SDK will be run in the background.
  and detect the beacon and provide appropriate actions.</p>
  <p>My implementation is that, if the application initializes my SDK with the foreground service &quot;OFF&quot;
  Then sometime later, when the application is in the background and trying to start the foreground service from the background this exception throws.
  The foreground service-related decisions are held by the server-side API. I'm periodically checking whether the server-side value is changed or not, and if the value is changed the changed action is reflected in the SDK.</p>