gpt4 book ai didi

android - 如何使用 Android 11 中另一个前台服务的打开相机运行前台服务?

转载 作者:行者123 更新时间:2023-12-04 13:54:16 24 4
gpt4 key购买 nike

我正在测试 foreground service 中的打开相机在 Android 11 中,我对新的 Android 11 限制有疑问:https://developer.android.com/guide/components/foreground-services
伪代码:

//Service1 is started by JobScheduler.
class Service1 extends Service {
...
startForeground(ID_OF_SERVICE1_NOTIFICATION, getService1Notification())
...
//Run another foreground service with open camera
Intent i = new Intent(getApplicationContext(), Service2.class)
ContextCompat.startForegroundService(context, i)
...
}

class Service2 extends Service {
...
startForeground(ID_OF_SERVICE2_NOTIFICATION, getService2Notification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST);
openCamera() // <-- Policy exception
...
}
类 Service1 由 JobScheduler 启动与 startForeground()并向用户显示通知。 Service1 可以用 ContextCompat.startForegroundService() 启动 ( startForeground()) Service2并向用户显示通知。 Service2 打开相机。用户一直看到通知。
list 编辑:
<service android:name=".service.Service2"
android:foregroundServiceType="camera|microphone"
android:stopWithTask="false"/>
已编辑 starForeground()Service2带标志 FOREGROUND_SERVICE_TYPE_MANIFEST : (一个特殊的值表示使用 list 文件中设置的所有类型)
来自 Logcat 的结果:

Foreground service started from background can not have location/camera/microphone access: service com.example.test/.service.Service2


也试过 FOREGROUND_SERVICE_TYPE_CAMERA|FOREGROUND_SERVICE_TYPE_MICROPHONE .
我想保持自动化为用户启动相机。
有什么办法吗?

最佳答案

据我所知,自 android 11 以来,如果应用程序在应用程序进程启动后对用户不可见,则无法从前台服务自动启动相机。用户必须 互动 与应用程序,以便能够使用相机。根据docs ,这些是被视为与应用交互的方式,而无需实际打开 Activity :

  • The service is started by a system component.
  • The service is started by interacting with app widgets.
  • The service is started by interacting with a notification. [note: Interacting! It is not enough to display the notification]
  • The service is started as a PendingIntent that is sent from a different, visible app.
  • The service is started by an app that is a device policy controller that is running in device owner mode.
  • The service is started by an app which provides the VoiceInteractionService. The service is started by an app that has the START_ACTIVITIES_FROM_BACKGROUND privileged permission.

关于android - 如何使用 Android 11 中另一个前台服务的打开相机运行前台服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65416622/

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