How to detect if Audiorecord is disabled by a third-party management app, attach demo

Source: Internet
Author: User
Tags try catch

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.