title: What's the point of a foreground service to record audio in Android 11?
body: <p>On one of my apps activities a user can start an <code>AudioSession</code>. This <code>AudioSession</code> runs until the user explicitly stops it in the app, or the app is terminated. During an <code>AudioSession</code>, <a href="https://developer.android.com/reference/android/media/AudioRecord" rel="nofollow noreferrer">AudioRecord</a> and <a href="https://developer.android.com/reference/android/media/AudioTrack" rel="nofollow noreferrer">AudioTrack</a> are used to record and play audio. Recording and playing should continue even if my app is minimized or the screen goes off.</p>
  <p>As far as I understand I want to record audio in background. There are several restrictions on this in different Android versions (correct me if I'm wrong):</p>
  <ul>
  <li>In the past recording audio was possible in any kind of service</li>
  <li>Starting Android 9 audio recording can no longer happen in a service other than a foreground service:</li>
  </ul>
  <blockquote>
  <p>One more change was added in Android 9: only apps running in the foreground (or a foreground service) could capture the audio input. <a href="https://developer.android.com/guide/topics/media/sharing-audio-input#pre-behavior" rel="nofollow noreferrer">1</a></p>
  </blockquote>
  <ul>
  <li>Starting Android 11, the behaviour does not make sense at all, as explained below.</li>
  </ul>
  <p>So below is an exceed of the <a href="https://developer.android.com/guide/components/foreground-services#types" rel="nofollow noreferrer">official Foreground services documentation</a>, omissions are marked with [...].</p>
  <blockquote>
  <h2>Declare foreground service types</h2>
  <p>[...]</p>
  <p>If your app targets Android 11 (API level 30) or higher and accesses the camera or microphone in a foreground service, declare the camera or microphone foreground service types, respectively, as attributes of your  component.</p>
  <p>Note: Although adding a foreground service type gives a foreground service the capability to access location, the camera, or the microphone, this foreground service is still affected by the access restrictions that were introduced in Android 11.</p>
  <p>[...]</p>
  <h2>Restricted access to location, camera, and microphone</h2>
  <p>To help protect user privacy, Android 11 (API level 30) introduces limitations to when a foreground service can access the device's location, camera, or microphone. The limitations depend on the app's state when the service starts.</p>
  <h3>Restrictions applied to services that start while an app is running in the background</h3>
  <p>On Android 11 and higher, if your app starts a foreground service while the app is running in the background, the service has the following access restrictions:</p>
  <p>[...]</p>
  <ul>
  <li>The service cannot access the microphone or camera.</li>
  </ul>
  <p>[...]</p>
  <h3>Restrictions applied to services that start while an app is running in the foreground</h3>
  <p>If your app starts a foreground service while the app is running in the foreground (&quot;while-in-use&quot;), the service has the following access restrictions:</p>
  <p>[...]</p>
  <ul>
  <li>If the user has granted the RECORD_AUDIO permission to your app, the service can access the microphone only while the app is running in the foreground.</li>
  </ul>
  </blockquote>
  <p>To conclude the above: If my foreground service is started while my app is in background, I can not record audio. If my foreground service is started while my app is foreground (&quot;while-in-use&quot;), I can only record audio while my app is foreground (&quot;while-in-use&quot;).</p>
  <p>So now my question is: Why would I use a foreground service if it has literally no influence on audio recording on Android 11+? I can just record audio, without a foreground service in my app to achieve exactly the same behaviour as with a foreground service. What am I missing?</p>