Problem description

We may encounter in at ordinary times in the development and application of < USES – permission android: name = “android. Permission. RECORD_AUDIO” / > to disable the third party, such as the millet’s own “security center” to disable, How do I detect if AudioRecord is disabled by a third-party management application?

There are many schemes on the Internet such as

Method one:

MediaRecorder can add a try catch to the prepare and Start methods to determine whether the recording permission has been obtained. However, this trick does not work on AudioRecord and does not throw an exception.

Method 2:

public boolean checkRecordAudioPermission() { 
      PackageManager pm = getPackageManager(); 
      boolean permission = (PackageManager.PERMISSION_GRANTED 
          == pm.checkPermission("android.permission.RECORD_AUDIO"."packageName"));
      returnpermission; }Copy the code

But you will find that the result is always true. This can only determine whether Manifest has registered permissions, not whether permissions are disabled by the system.

Method 3:

The Context. CheckSelfPermission (permission_name), etc. But in the end can’t solve the problem

The ultimate solution

Based on the decibel value, determine whether the recording permission of the current application is prohibited by third party application source code

  • Description: RecordAudioPermissionDetect key classes
  • Use:
public class MainActivity extends AppCompatActivity implements RecordAudioPermissionDetect.onPermitRecordListener { 
      private static final String TAG = "MainActivity"; 
      private RecordAudioPermissionDetect 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 = new RecordAudioPermissionDetect(this); 
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
            fab.setOnClickListener(new View.OnClickListener() { 
                 @Override public void onClick(View view) { mRecordAudioPermissionDetect.startCheckRecordPermission(); }}); }@Override 
      protected void onResume(a) { 
             super.onResume(); 
             mRecordAudioPermissionDetect.startCheckRecordPermission();
       } 
      @Override 
      protected void onPause(a) { 
            super.onPause(); 
            mRecordAudioPermissionDetect.stopCheck(); 
      } 
      @Override 
      public void isPermit(boolean flag) { 
            if(! flag) { mRecordAudioPermissionDetect.showMissingPermissionDialog(this); 
            } else { 
            //TODO recalls its recording operation}}}Copy the code

Note: In the process of use, because a device has only one microphone resource, we mainly call our own recording operation in the isPermit method.