Android 11/R - 使用从后台启动的前台服务警报接收器开始录制音频/视频

问题描述

在使用 Android R/targetSdkVersion 30 时,有没有办法在前台服务从后台启动时开始录制音频或视频,例如警报接收器?

也许我做错了什么或不明白如何安排闹钟? 他们不希望应用程序在没有用户交互的情况下开始录制。当用户按下按钮或通知时,录制运行正常。他们说我可以使用 PendindIntent - 所以我用它来设置一个稍后启动服务的警报,不是吗?但是当警报接收器启动服务并尝试录制时 - 它被此消息阻止: "从后台启动的前台服务不能有
位置/相机/麦克风访问“

他们声明:

When a foreground service starts in one of the following situations,the service is exempt from the restrictions on while-in-use access to location,camera,and microphone:
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.
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. 

我不完全理解的选项,也许可以解开这个谜:

  1. 立即运行服务,直到警报响起。它可以工作,但会耗尽电池电量。
  2. “VoiceInteractionService”作为服务的启动还是将服务转换为 VoiceInteractionService?
  3. 将服务更改为“AccessibilityService” - 我读到 google play 不喜欢它。
  4. 使用 JobScheduler 而不是 alarmreceiver 或其他 Worker 类型?

我试过这样的直接报警:

 Intent intent = new Intent(mContext,MainService.class);
 intent.setAction(Constants.ACTION.ALARM_START_AND_RECORD);    
 PendingIntent pendingIntent = PendingIntent.getService(mContext,intent,0);
 alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);

或者使用报警接收器:

Intent intent = new Intent(mContext,AlarmReceiver.class);
intent.setAction(Constants.ACTION.ALARM_START_AND_RECORD);
pendingIntent = PendingIntent.getbroadcast(this.getActivity(),REQUEST_SET_ALARM,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,pendingIntent);

定位 sdk 29 (targetSdkVersion 29) 没有帮助,它之前有帮助,但看起来他们已经改变了上一个 android 11 :-(

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...