1、检查权限
private boolean checkPremission(String[] needPermission) {
if (needPermission==null||needPermission.length==0){
return false;
}
boolean allGranted = true;
for (String permission : needPermission) {
allGranted &= ContextCompat.checkSelfPermission(this,permission)== PackageManager.PERMISSION_GRANTED;
}
return allGranted;
}
2、获取权限
if ( !checkPremission(NEED_PERMISSION)){
ActivityCompat.requestPermissions(MainActivity.this,NEED_PERMISSION,ACTION_REQUEST_PERMISSIONS);
}
3、权限返回结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == ACTION_REQUEST_PERMISSIONS){
}
}