I have seen multiple questions on SO that say it is impossible to see when another app wants to use the microphone (Unable to access microphone when another app is using it in Android). However, I know it is possible because Shazam has an "Auto" function that allows the exact functionality I want: to continuously listen to audio when other apps are not using the microphone.
Currently, when I use my app and use an app like Snapchat, I am unable to record a video with audio because Snapchat does not take the mic over. However, as I said before, in this case Shazam's Auto feature works fine.
So how do I listen for when other apps want to use the mic? I am fine using something that is a "hack" as long as it does not require rooting the phone or similar.
EDIT: Shazam never had this functionality, their app fails to relinquish the mic to other applications while it is running.
This is currently the best solution I have. There is no way to listen for other applications requesting the microphone, so I created a
UsageStats
checker that runs every 3 seconds to see if the currently open app has the ability to request audio. Please let me know if you have something better or make improvements to this.Note: You must add the permissions to the app manifest:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />