gpt4 book ai didi

android - 在 Android 11 中录制音频的前台服务有什么意义?

转载 作者:行者123 更新时间:2023-12-05 08:04:33 24 4
gpt4 key购买 nike

在我的一个应用程序 Activity 中,用户可以启动 AudioSession。此 AudioSession 一直运行,直到用户在应用中明确停止它,或者应用终止。在 AudioSession 期间,AudioRecordAudioTrack用于录制和播放音频。即使我的应用程序最小化或屏幕关闭,录制和播放也应该继续。

据我所知,我想在后台录制音频。在不同的 Android 版本中对此有一些限制(如果我错了请纠正我):

  • 过去,任何类型的服务都可以录制音频
  • 启动 Android 9 录音不会再发生在前台服务以外的服务中:

One more change was added in Android 9: only apps running in the foreground (or a foreground service) could capture the audio input. 1

  • 从 Android 11 开始,该行为根本没有意义,如下所述。

下面是 official Foreground services documentation 的一个超出部分, 遗漏用 [...] 标记。

Declare foreground service types

[...]

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.

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.

[...]

Restricted access to location, camera, and microphone

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.

Restrictions applied to services that start while an app is running in the background

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:

[...]

  • The service cannot access the microphone or camera.

[...]

Restrictions applied to services that start while an app is running in the foreground

If your app starts a foreground service while the app is running in the foreground ("while-in-use"), the service has the following access restrictions:

[...]

  • 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.

综上所述:如果我的前台服务在我的应用程序处于后台时启动,我将无法录制音频。如果我的前台服务在我的应用处于前台(“使用中”)时启动,我只能在我的应用处于前台(“使用中”)时录制音频。

现在我的问题是:如果前台服务对 Android 11+ 上的音频录制几乎没有影响,我为什么要使用它?我可以只录制音频,而无需在我的应用程序中使用前台服务来实现与使用前台服务完全相同的行为。我错过了什么?

最佳答案

Exemptions from the restrictions

In some situations, even if a foreground service is started while the app is running in the background, it can still access location, camera, and microphone information while the app is running in the foreground ("while-in-use"). In these same situations, if the service declares a foreground service type of location and is started by an app that has the ACCESS_BACKGROUND_LOCATION permission, this service can access location information all the time, even when the app is running in the background.

The following list contains these situations:
  1. The service is started by a system component.
  2. The service is started by interacting with app widgets.
  3. The service is started by interacting with a notification.
  4. The service is started as a PendingIntent that is sent from a different, visible app.
  5. The service is started by an app that is a device policy controller that is running in device owner mode.
  6. The service is started by an app which provides the VoiceInteractionService.
  7. The service is started by an app that has the START_ACTIVITIES_FROM_BACKGROUND privileged permission.

我正在尝试 3. 但未成功。你过得怎么样?

关于android - 在 Android 11 中录制音频的前台服务有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68177773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com