Access Microphone / Camera in background in Android 11 - android

Access to the Microphone in the background is stopped in Android 11. There are only 3 options, Allow when in-use, Allow once, and Deny.
How to make the app get access to the microphone in the background all the time in Android 11? Is there any workaround?

Now we have to specified a type for our foreground service (https://developer.android.com/guide/components/foreground-services#types):
<manifest>
...
<service ...
android:foregroundServiceType="camera|microphone" />
</manifest>
But in some cases our foreground service can't still access camera or microphone even if we specified android:foregroundServiceType:
If a foreground service was started while app was in background (wasn't visible to a user - no visible activities), for example on device boot (BOOT_COMPLETED) broadcast, then such service can't start using camera, microphone
If a foreground service was started while app was in foreground (was visible to a user - some visible activity) then such service can start using camera, microphone
Info from: https://developer.android.com/guide/components/foreground-services#bg-access-restrictions
My issue Camera2 cameraManager.openCamera exception from time to time on some devices

The new Android Developer Policy restricts access to Microphone and Camera in the background.
Using Accessibility Service one can still use the feature in the background. However, the notification will always be on.

To access background mic / camera / location in Android 11 there are some exemptions given by Android , you need to qualify one of these conditions:
Refer: https://developer.android.com/guide/components/foreground-services#restrictions-exemptions
Exemptions to while-in-use restrictions:
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.

Related

Foreground service being killed on Chinese OEMs like Vivo

One of core functionalites of our app, requires us to track the drivers all the time in working hours. The foreground service, used for fetching location updates, is being killed in some chineese devices like vivo. I am aware of these custom OS killing services to save battery, I was wandering has there been any recent development on this topic. Is there any way to make sure that the service won't be killed on most devices and If I ask user to give auto start functionality, is there any way to check if the user has given the permission?
I have started the service sticky and obtained wake lock to prevent against doze mode. Also, Showing dialog to obtain auto start functionality if available for manufacturer.
WakeLock doesn't really help on last Android versions unless you request REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission before acquiring.

Android Q - Background Location Permission needed for Foreground service?

The first point in this document says that Background Location permission is required if the foreground location service is started while the app is in foreground starting Android 11.
https://developer.android.com/guide/components/foreground-services#bg-access-restrictions
My use case is that a user taps on a button to start location tracking. Tapping that button starts a foreground service which puts a notification. Now, I want the app to continue tracking even after backgrounding.
Until Android 10, background location permission wasn't required to accomplish this.
My question is that to support Android 11, do I need to start requesting background permission as well? Also, do I need to do the same for Android 10? Not finding any other reference on the internet to verify this. please let me know your thoughts.
According to the definition of background work
An app is considered to be running in the background as long as each of the following conditions are satisfied:
None of the app's activities are currently visible to the user.
The app isn't running any foreground services that started while an activity from the app was visible to the user.
Otherwise, the app is considered to be running in the foreground.
In your case, you fit into the second point where you started a foreground service when an activity is visible to the user so technically your app is still considered to be in the foreground while the service notification is being shown even if the user navigates away from your app.
The foreground service needs to have android:foregroundServiceType="location" in its manifest declaration if you are targeting Android 10 and above. See here.
You don't necessarily need the ACCESS_BACKGROUND_LOCATION permission if you are fetching location updates in the foreground service that you have created since you are not technically accessing location while in the background. However, within your service, if you use any APIs that may require background location permission, such as Geofencing, then you will require the background permission to be added to the manifest and request and handle the permission accordingly.

Android 9: Background Restriction App Setting

I'm working on a music player app and I'm noticing weird behavior on Android 9 devices when a user enables the "Background Restriction" setting (Settings -> Apps -> [App Name] -> Battery -> Background Restriction).
Here's what I'm doing:
I start my music player service by calling Service.startService() then set it to foreground via Service.startForeground() while my app is in the foreground.
Here's what I'm seeing when "Background Restriction" is turned on:
1) Service.startForeground() will not posting a notification
2) My foreground service is killed by the OS within a minute of my app going to the background
Here's what I see in the logs:
1) "Service.startForeground() not allowed due to bg restriction" when calling Service.startForeground()
2) "Stopping service due to app idle" when my app is auto-killed by the OS
Here's my question:
I thought the whole point of a foreground service is to allow background processing with the user's knowledge (an ongoing notification); is the "Background Restriction" setting really intended to disallow all background activity?
Interesting find:
Looking at Google's "Universal Music Player" sample project on GitHub, I noticed that their sample project is not being killed like my app is. After digging I noticed this is because they are binding to their service and never unbinding in Activity.onPause(). According to Google's docs, bound services are not subject to the same background restrictions. Is this really the solution to my problem? Seems a little hacky/fragile.
Thanks in advance for the help!
Here's what I've found:
"Background Restriction" (or "Allow Background Activity" on some devices) is intended to stop ALL background activity regardless of whether your service has called setForeground()
There is no way around this setting. You cannot programmatically disable it. Your only option is to programmatically check if it's enabled using ActivityManager.isBackgroundRestricted() and display a pop-up informing your users on how to disable this setting
Google's Universal Music Player sample project on GitHub happens to work (as of the writing of this answer) only because a service bind is not released when the main Activity is paused. The sample project's service is however killed when the main Activity is garbage collected (typically 30-45 minutes depending on the device).

Turn on/off flashlight while app is minimized

In my application I have an activity and a service. The service continously manages some data. In certain cases the service sends a broadcast request to the activity to turn on flashlight.
This works very well. However it does not work when app is minimized (manually or by incomming call). As soon as I bring the app back to the front it starts to work again.
As I observed the log I saw that the service is still running when app is minimized. However since the activity is not present anymore the camera object cannot be accessed to activate the flashlight. But there is no error in the log.
Why is the flashlight not activated? How can the flashlight be activated when the app is minimized?
Only the foreground activity can hold the camera. From the Google documentation:
"If your application does not properly release the camera, all subsequent attempts to access the camera, including those by your own application, will fail and may cause your or other applications to be shut down."
You can try claiming it on the service instead, but I suspect that won't work well. It just really wasn't designed to work this way.

Is it possible to record audio on Android while lock screen is on?

I'm writing a sound recorder app and I'm wondering if it's possible to activate the MediaRecorder while the phone has its lock screen up. If so, is it just a matter of a special permission or something else? Thanks in advance.
you should try using PowerManager class with permission android.permission.WAKE_LOCK.
you can find more Here.
Yes it is possible. Lock screen does not affect the working of services in an application. So if you are using a service, then your recording will proceed. However, if your phone is in standby, then it may not work. In that case, just grab a wakelock to keep the CPU alive and function your needs. However, grabbing a wakelock affects battery, so you must use it intelligently.
These days (Android 11 or later), this should no longer be possible:
Note: On devices running Android 9 (API level 28) or higher, apps running in the background cannot access the microphone. Therefore, your app should record audio only when it's in the foreground or when you include an instance of MediaRecorder in a foreground service.
[1] https://developer.android.com/guide/topics/media/mediarecorder
Restricted access to location, camera, and microphone
To help protect user privacy, Android 11 (API level 30) introduces limitations to when a foreground service can access the device's location, camera, or microphone. When your app starts a foreground service while the app is running in the background, the foreground service has the following limitations:
...
The foreground service cannot access the microphone or camera.
[2] https://developer.android.com/guide/components/foreground-services

Categories

Resources