首页 > 解决方案 > Android 11 中的后台麦克风访问引发异常

问题描述

Microphone通过 Android 应用程序的后台访问Android 11在 Logcat 中引发以下异常:

W/ActivityManager: Foreground service started from background can not have location/camera/microphone access

相同的代码在 Android 10 及更低版本中完美运行。如何解决?

标签: androidmicrophoneandroid-11

解决方案


在后台访问位置或摄像头或麦克风的服务Android 11需要在文件中添加以下内容:AndroidManifest.xml

<manifest>
    ...
    <service ...
        android:foregroundServiceType="location|camera|microphone" />
</manifest>

另外,在方法中添加以下内容startForeground

Service.startForeground(notification,
        FOREGROUND_SERVICE_TYPE_LOCATION | FOREGROUND_SERVICE_TYPE_CAMERA | FOREGROUND_SERVICE_TYPE_MICROPHONE);

但即便如此,Android 11 也不允许应用在后台获取麦克风/摄像头访问权限。唯一的解决方案是使用辅助功能。之后它将像以前一样工作。但是如果可以安全使用,Google Play 应用程序必须在使用辅助功能之前仔细检查政策,否则您可能会冒着应用程序因使用变通方法而被暂停的风险。


推荐阅读