I am testing now open camera in foreground service in Android 11 and I have problem with new Android 11 restrictions: https://developer.android.com/guide/components/foreground-services
Pseudo code:
//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
...
}
Class Service1 is started by JobScheduler with startForeground() and show notification to user. Service1 can start (ContextCompat.startForegroundService()) Service2 with startForeground() and show notification to user too. Service2 opens camera. The user sees notification all the time.
manifest edited:
<service android:name=".service.Service2"
android:foregroundServiceType="camera|microphone"
android:stopWithTask="false"/>
Edited starForeground() in Service2 with flag FOREGROUND_SERVICE_TYPE_MANIFEST: (a special value indicates to use all types set in manifest file)
The result from Logcat:
Foreground service started from background can not have location/camera/microphone access: service com.example.test/.service.Service2
Tried also FOREGROUND_SERVICE_TYPE_CAMERA|FOREGROUND_SERVICE_TYPE_MICROPHONE.
I would like to keep the automation in starting the camera for user. Is there any way?