Android P Will Prevent Background Apps From Accessing the Microphone
The yet-to-be-named Android P is still a few months away from hitting devices, but we already have a fair idea of what changes it'll bring to the table. We know that it'll have a built-in call recorder, let carriers manipulate how the signal bar is calibrated and most recently, deny camera access to apps running in the background.
Technically any app that you gave permission to access your device’s microphones could have been running in the background. It could also be recording anything you say without you realizing it. However, Android Oreo’s limitations on background services made it a bit harder to implement. But, with some tweaking, it was still possible for an app to record audio in the background without you knowing. Which is why Android P is implementing safeguards to limit apps' background activities.
How it works
When an apps UID enters an idle state, Android’s audio system won’t allow it to record audio. The mechanism is slightly different from the one used to prevent background apps from accessing the camera. Instead of generating an error, the microphone will report empty data. Once the app becomes active again, it’ll start recording audio as intended.
The new system will prevent a hypothetical malicious app from recording audio once the app detects that it is in the idle state. Apps that secretly access your phone’s microphone are very real and several studies have confirmed that they do indeed exist, often in forms that you wouldn't expect.
It is understandable if an app designed to record audio requests microphone permissions, but when you have a flashlight app that's requesting camera and location access, it should set off some flags. Unfortunately, many people fail to pay attention to it and fall prey to malware. The new system in Android P is a nice improvement over flat out denying the app access to a service, as it saves you the hassle of manually fidgeting with the permissions every time.
Source: XDA developers