NO IMAGE

操蛋的Vivo 許可權自動返回授權 這你們都懂的不懂的戳下面

點選開啟連結

現在來說說如何破?網上好多垃圾資源 我這裡提供一條 適配 vivo 6.0的方案 直接上程式碼

/**
* Created by Fashionaly on 2018/7/13.
*/
public class PermissionUtil {
private Context mContext;
private PermissionUtil() {
}
public static PermissionUtil getInstance() {
return PermissionUtilHolder.sInstance;
}
public PermissionUtil with(Context context) {
mContext = context.getApplicationContext();
return PermissionUtilHolder.sInstance;
}
private static class PermissionUtilHolder {
private static final PermissionUtil sInstance = new PermissionUtil();
}
public boolean TestPermission() {
try {
if (!isCameraCanUse()) {
return false;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!isHasCameraPermission()) {
return false;
}
}
if (!isHasRecordPermission(mContext)) {
return false;
}
} catch (Exception ex) {
return false;
}
return true;
}
public static boolean isCameraCanUse() {
boolean canUse = true;
Camera mCamera = null;
try {
mCamera = Camera.open();
// setParameters 是針對魅族MX5 做的。MX5 通過Camera.open() 拿到的Camera
// 物件不為null
Camera.Parameters mParameters = mCamera.getParameters();
mCamera.setParameters(mParameters);
} catch (Exception e) {
canUse = false;
}
if (mCamera != null) {
mCamera.release();
}
return canUse;
}
public static boolean isHasCameraPermission() {
Field fieldPassword = null;
try {
Camera camera = Camera.open();
fieldPassword = camera.getClass().getDeclaredField("mHasPermission");
fieldPassword.setAccessible(true);
Boolean result = (Boolean) fieldPassword.get(camera);
return result;
} catch (Exception e) {
e.printStackTrace();
return true;
}
}
/**
* 判斷是是否有錄音許可權
*/
public static boolean isHasRecordPermission(final Context context) {
// 音訊獲取源
int audioSource = MediaRecorder.AudioSource.MIC;
// 設定音訊取樣率,44100是目前的標準,但是某些裝置仍然支援22050,16000,11025
int sampleRateInHz = 44100;
// 設定音訊的錄製的聲道CHANNEL_IN_STEREO為雙聲道,CHANNEL_CONFIGURATION_MONO為單聲道
int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
// 音訊資料格式:PCM 16位每個樣本。保證裝置支援。PCM 8位每個樣本。不一定能得到裝置支援。
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
// 緩衝區位元組大小
int bufferSizeInBytes = 0;
bufferSizeInBytes = 0;
bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
channelConfig, audioFormat);
AudioRecord audioRecord = new AudioRecord(audioSource, sampleRateInHz,
channelConfig, audioFormat, bufferSizeInBytes);
//開始錄製音訊
try {
// 防止某些手機崩潰,例如聯想
audioRecord.startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
}
/**
* 根據開始錄音判斷是否有錄音許可權
*/
if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
return false;
}
audioRecord.stop();
audioRecord.release();
audioRecord = null;
return true;
}
}
    List<String> mPermissionList = new ArrayList<>();
String[] permissions = new String[]{Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE
};

    private boolean getPermissionJudgeCanPass() {
mPermissionList.clear();
for (int i = 0; i < permissions.length; i  ) {
if (ContextCompat.checkSelfPermission(CloudVideoActivity.this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);
}
}
if (mPermissionList.isEmpty()) {//未授予的許可權為空,表示都授予了
//            Toast.makeText(CloudVideoActivity.this, "許可權授權完畢", Toast.LENGTH_LONG).show();
return true;
} else {//請求許可權方法
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//將List轉為陣列
ActivityCompat.requestPermissions(CloudVideoActivity.this, permissions, MY_PERMISSIONS_REQUEST_CALL_CAMERA);
return false;
}
}

最後呼叫

 if (getPermissionJudgeCanPass()) {
try {
if (PermissionUtil.getInstance().with(this).TestPermission()) {
Intent intent = VideoRecordActivity.startRecordActivity(videoPath, CloudVideoActivity.this);
startActivityForResult(intent, BACK);
} else {
Toast.makeText(this, "為了程式正常執行請授權應用相關許可權(儲存、相機、麥克風)", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, "初始化拍攝失敗", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "為了程式正常執行請授權應用相關許可權(儲存、相機、麥克風)", Toast.LENGTH_SHORT).show();
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//將List轉為陣列
ActivityCompat.requestPermissions(CloudVideoActivity.this, permissions, MY_PERMISSIONS_REQUEST_CALL_CAMERA);
}