Android 6.0 is invalid solve some phones dynamic application permissions, and 6.0 do not have permission to apply the following issues

scene one:

Description: Some Android phones, although the system is 6.0, but the dynamic applied for permission, or do not have permission pop;

Analysis: Although the phone is a 6.0 system, but prior to or use of the mechanism. Only when actually used to execute permissions and relevant code, only the pop-up permissions prompt box;

 

Scene Two:

Description: Android 6.0 application permissions, click the "Do not ask me after not allowed", and then check the next time, will not go callback;

 

Scene Three:

Description: Android 6.0, no permissions apply only when used, will pop up; once the user has denied permission to monitor, will lead to various exceptions;

 

For the above scenario, the solution is given below: Analog start the camera, open the analog recording

Analog camera to be launched:

public static boolean hasCameraPermission() {
        try {
            Camera camera = Camera.open(0);
            try {
                Camera.Parameters param = camera.getParameters();
                if (param != null) {
                    camera.release();
                    camera = null;
                    return true;
                } else {
                    camera.release();
                    camera = null;
                    return false;
                }
            } catch (Exception e) {
                camera.release();
                camera = null;
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

Analog punch recording:

public static boolean hasRecordPermission() {
        int minBufferSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        int bufferSizeInBytes = 640;
        byte[] audioData = new byte[bufferSizeInBytes];
        int readSize = 0;
        AudioRecord audioRecord = null;
        try {
            audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 8000,
                    AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
            // 开始录音
            audioRecord.startRecording();
        } catch (Exception e) {
            //可能情况一
            if (audioRecord != null) {
                audioRecord.release();
                audioRecord = null;
            }
            return false;
        }
        // 检测是否在录音中,6.0以下会返回此状态
        if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
            //可能情况二
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;
            }
            return false;
        } else {// 正在录音
            readSize = audioRecord.read(audioData, 0, bufferSizeInBytes);
            // 检测是否可以获取录音结果
            if (readSize <= 0) {
                //可能情况三
                if (audioRecord != null) {
                    audioRecord.stop();
                    audioRecord.release();
                    audioRecord = null;
                }
                return false;
            } else {
                //有权限,正常启动录音并有数据
                if (audioRecord != null) {
                    audioRecord.stop();
                    audioRecord.release();
                    audioRecord = null;
                }
                return true;
            }
        }
    }

Written in the last:

This method has a problem, if the camera or recording was occupied by other applications, here also returns a failure, the other authority is there, the need to optimize

 

Completed. . .

Published 33 original articles · won praise 20 · views 80000 +

Guess you like

Origin blog.csdn.net/huangwenkui1990/article/details/83031266