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:
- run the service immediately and until the alarm. It works but it derains the battery.
- "VoiceInteractionService" as a start up for the service or convert the service to be VoiceInteractionService?
- change the service to be "AccessibilityService" - I read that google play do not like it.
- 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 :-(