This article describes the method of Android programming to detect whether the recording permissions of mobile phones are turned on. Share it for your reference, as follows:
Permission detection before 6.0 just detects whether it is registered in the manifest file
Boolean flag = (PackageManager.PERMISSION_GRANTED == (".RECORD_AUDIO", "Bail Name")); Boolean flag = (this, .)== PermissionChecker.PERMISSION_GRANTED; if (flag){ ("Permissions"); } else { ("No permission"); return; }
In the 6.0 system, you can get permissions whether you close or open the recording permissions of the app. It is normal for 6.0 or above. Later, it was found that Google strengthened permission management after 6.0.
public class CheckPermissionUtils { /** *Whether the recording permission of the app is turned on *Android 6.0 version or above traditional methods are not suitable for this * @param context */ public static Boolean isHasAudioRecordPermission(Context context) { // Audio acquisition source int audioSource = ; // Set the audio sampling rate, 44100 is the current standard, but some devices still support 22050, 16000, 11025 int sampleRateInHz = 44100; // Set the audio recording channel CHANNEL_IN_STEREO as dual channel, CHANNEL_CONFIGURATION_MONO as mono channel int channelConfig = AudioFormat.CHANNEL_IN_STEREO; // Audio data format: PCM 16 bits per sample. Ensure equipment support. PCM 8 bits per sample. It may not be supported by the device. int audioFormat = AudioFormat.ENCODING_PCM_16BIT; // Buffer byte size int bufferSizeInBytes = 0; bufferSizeInBytes = (sampleRateInHz, channelConfig, audioFormat); AudioRecord audioRecord = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes); //Start audio recording try{ // Prevent some mobile phones from crashing, such as Lenovo (); } catch (IllegalStateException e){ (); } /** * Determine whether you have recording permission based on the start recording */ if (() != AudioRecord.RECORDSTATE_RECORDING) { return false; } (); (); audioRecord = null; return true; } }
The last one, there is no problem with the test.
public class CheckPermissionUtils { private static final String TAG = "CheckPermissionUtils"; private static CheckPermissionUtils checkPermissionUtils = new CheckPermissionUtils(); static final int SAMPLE_RATE_IN_HZ = 44100; static final int BUFFER_SIZE = ( SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT); private AudioRecord mAudioRecord; Boolean isGetVoiceRun; private Object mLock; private int count=0; private Boolean isHasPermission; private CheckPermissionUtils() { mLock = new Object(); } public static CheckPermissionUtils getinstance() { if (checkPermissionUtils == null) { checkPermissionUtils = new CheckPermissionUtils(); } return checkPermissionUtils; } public Boolean isHasAudioRecordingPermission(Context context) { isHasPermission=false; count=0; mAudioRecord = new AudioRecord(, SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT, BUFFER_SIZE); if (mAudioRecord == null) { ("sound", "mAudioRecord initialization failed"); } isGetVoiceRun = true; try { // new Thread(new Runnable() { // @Override // public void run() { (); short[] buffer = new short[BUFFER_SIZE]; while (isGetVoiceRun) { count++; if (count++>10) { isGetVoiceRun=false; } //r is the actual read data length, generally speaking, r will be less than buffersize int r = (buffer, 0, BUFFER_SIZE); long v = 0; // Take out the buffer content and perform square sum operation for (int i = 0; i < ; i++) { v += buffer[i] * buffer[i]; } // Divide the sum of squares by the total length of the data to get the volume. double mean = v / (double) r; double volume = 10 * Math.log10(mean); (TAG, "------------------------------------------------------------------------------------------------------------------------------ +volume+"----v"+v+"------r"+r ); if (v>0&&r>0) { //There is a recording isHasPermission=true; return isHasPermission; } // About ten times a second synchronized (mLock) { try { (5); } catch (InterruptedException e) { (); } } } (); (); mAudioRecord = null; // } //}).start(); } catch (Exception e) { } return isHasPermission; } }
Also, you can click here to viewAndroid permissions operation instructions
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android operating json format data skills》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.