红米小米等手机在android6.0以下加了权限管理,用android自身带的checkpermisson方法时,永远返回true,因此在获取权限为true时,加入判断
录音权限:
Log.e("MainActivity", "onPermissionGranted");
if (PermissionHelper.isAudioEnable()) {
Toast.makeText(MainActivity.this, "开始录音操作", Toast.LENGTH_LONG).show();
} else {
DialogUtil.showPermissionManagerDialog(MainActivity.this, "录音或麦克风");
}
帮助类:
package com.andsync.xpermissionutils.util;
import android.content.Context;
import android.hardware.Camera;
import android.location.LocationManager;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
/**
* 可能会有用的一些方法
*
* @author AndSync
* @date 2017/10/30
* Copyright © 2014-2017 AndSync All rights reserved.
*/
public class PermissionHelper {
/**
* 6.0以下判断是否开启录音权限
*/
public static boolean isAudioEnable() {
boolean isValid = true;
AudioRecord mRecorder;
int bufferSize =
AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
short[] mBuffer = new short[bufferSize];
mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
//开始录制音频
try {
// 防止某些手机崩溃,例如联想
mRecorder.startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
isValid = false;
return isValid;
} finally {
// TODO
}
int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
if (AudioRecord.ERROR_INVALID_OPERATION != readSize) {
// 做正常的录音处理
} else {
//录音可能被禁用了,做出适当的提示
isValid = false;
}
// 停止录制
try {
// 防止某些手机崩溃,例如联想
if (mRecorder != null) {
// 停止
mRecorder.stop();
mRecorder.release();
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
return isValid;
}
/**
* 6.0以下判断是否开启相机权限
*/
public static boolean isCameraEnable() {
boolean isCanUse = true;
Camera mCamera = null;
try {
mCamera = Camera.open();
//针对魅族手机
Camera.Parameters mParameters = mCamera.getParameters();
mCamera.setParameters(mParameters);
} catch (Exception e) {
isCanUse = false;
}
if (mCamera != null) {
try {
mCamera.release();
} catch (Exception e) {
e.printStackTrace();
return isCanUse;
}
}
return isCanUse;
}
/**
* 手机是否开启位置服务,如果没有开启那么所有app将不能使用定位功能
*/
public static boolean isLocServiceEnable(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}
return false;
}
}