Problem description
We may encounter in the development, the application of <uses-permission android:name= "Android.permission.RECORD_AUDIO"/> was disabled by the third party, If the Xiaomi's own "Security Center" to disable, then how to detect Audiorecord is the third-party management application to disable it?
There are many programs on the internet, such as
Method One:
Mediarecorder can determine if the recording permission is obtained by adding a try catch to the prepare and start methods. But this trick is not effective on the Audiorecord, it does not throw the exception.
Method Two:
publicbooleancheckRecordAudioPermission(){ PackageManager pm = getPackageManager(); boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.RECORD_AUDIO""packageName")); return permission;}
But you will find that the returned result is always true. It seems that this can only determine whether manifest has registration rights, but not to determine whether the system is disabled.
Method.. :
Context.checkselfpermission (permission_name) et cetera
But ultimately, it doesn't solve the problem.
The ultimate Solution
Determine if the current app's recording permissions are blocked by a third-party application based on the decibel value
Project address See [Https://github.com/Z644912187/RecordAudioPermissionDetect]
Description
Recordaudiopermissiondetect Key Classes
Use
Public class mainactivity extends appcompatactivity implements Recordaudiopermissiondetect. Onpermitrecordlistener { Private Static FinalString TAG ="Mainactivity";PrivateRecordaudiopermissiondetect Mrecordaudiopermissiondetect;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar); Setsupportactionbar (toolbar); Mrecordaudiopermissiondetect =NewRecordaudiopermissiondetect ( This); Floatingactionbutton fab = (Floatingactionbutton) Findviewbyid (R.id.fab); Fab.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View view) {mrecordaudiopermissiondetect.startcheckrecordpermission (); } }); }@Override protected void Onresume() {Super. Onresume (); Mrecordaudiopermissiondetect.startcheckrecordpermission (); }@Override protected void OnPause() {Super. OnPause (); Mrecordaudiopermissiondetect.stopcheck (); }@Override Public void Ispermit(BooleanFlag) {if(!flag) {Mrecordaudiopermissiondetect.showmissingpermissiondialog ( This); }Else{//todo Note recall your own recording operation} }}
Attention:
We used the process, because one device has only one microphone resource, so mainly in the Ispermit method to recall their own recording operations.
How to detect if Audiorecord is disabled by a third-party management app, attach demo