0

Is there a way to start recording audio or video from a foreground service when it starts from the background like alarm receiver while using Android R/targetSdkVersion 30?

Maybe I do something wrong or do not understand how to schedule alarms? They do not want app to start recording without user interaction. the recording runs ok while the user press a button or notification. they say that I can use PendindIntent - so I use it to set an alarm that will start the service later, no? but when the alarm receiver starts the service and try recording - it is being blocked by this message: "Foreground service started from background can not have
location/camera/microphone access"

They declare that:

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. 

Options which I do not fully understand and maybe solve this mystery:

  1. run the service immediately and until the alarm. It works but it derains the battery.
  2. "VoiceInteractionService" as a start up for the service or convert the service to be VoiceInteractionService?
  3. change the service to be "AccessibilityService" - I read that google play do not like it.
  4. using JobScheduler instead of alarmreceiver or ant other Worker type?

I have tried direct alarm like that:

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

Or using alarmreceiver:

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

Targeting sdk 29 (targetSdkVersion 29) does not help, it helped before but looks like last android 11 they have changed it :-(

kfir
  • 635
  • 8
  • 27
  • 1
    "Is there a way to start recording audio or video from a foreground service when it starts from the background like alarm receiver while using Android R/targetSdkVersion 30?" -- AFAIK, no. "they say that I can use PendindIntent" -- but they clarify that as needing to be "sent from a different, visible app", and `AlarmManager` is not visible. "It works but it derains the battery" -- a service, on its own, has little impact on the battery. It requires a foreground notification and has impact on system RAM, though. – CommonsWare Dec 29 '20 at 13:10
  • 1
    "using JobScheduler instead of alarmreceiver or ant other Worker type?" -- if any of those worked, that would represent a privacy flaw that is likely to be fixed, whether in a patch release or some future Android version. – CommonsWare Dec 29 '20 at 13:11

0 Answers0