Android - 运行时权限一个工具类搞定,让人茅塞顿开
private boolean isSensorsPermissionOk = false;
private boolean isSmsPermissionOk = false;
private boolean isStoragePermissionOk = false;
private boolean isCustomPermissionOk = false;
//当前未获取的权限列表
private List<String> mPermissionList = new ArrayList<>();
//自定义权限数组
private String[] mCustomPermissionArray;
//所有权限数组
private String[] mAllPermissionArray = new String[]{
PERMISSION_CALENDAR,
PERMISSION_CAMERA,
PERMISSION_CONTACTS,
PERMISSION_LOCATION,
PERMISSION_MICROPHONE,
PERMISSION_PHONE,
PERMISSION_SENSORS,
PERMISSION_SMS,
PERMISSION_STORAGE
};
private static class SingletonHolder {
public static PermissionManager instance = new PermissionManager();
}
private PermissionManager() {
}
public static PermissionManager getInstance() {
return SingletonHolder.instance;
}
/**
检查所有权限
@return true 已获取所有权限,false 未获取所有权限
*/
public boolean checkAllPermission(Context context) {
isAllPermissionOk = true;
mPermissionList.clear();
for (int i = 0; i < mAllPermissionArray.length; i++) {
if (ContextCompat.checkSelfPermission(context, mAllPermissionArray[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(mAllPermissionArray[i]);
isAllPermissionOk = false;
}
}
return isAllPermissionOk;
}
/**
获取所有权限
*/
public void getAllPermission(Activity activity) {
checkAllPermission(activity);
if (mPermissionList.isEmpty()) {
//未授予的权限为空,表示都授予了
} else {
//请求权限方法
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将 List 转为数组
ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE_ALL);
}
}
/**
获取所有权限
*/
public void getAllPermission(Fragment fragment) {
checkAllPermission(fragment.getContext());
if (mPermissionList.isEmpty()) {
//未授予的权限为空,表示都授予了
} else {
//请求权限方法
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将 List 转为数组
fragment.requestPermissions(permissions, REQUEST_CODE_ALL);
}
}
/**
检查自定义权限
@param customPermissionArray 自定义权限数组
@return true 已获取自定义权限,false 未获取自定义权限
*/
public boolean checkCustomPermission(Context context, String[] customPermissionArray) {
mCustomPermissionArray = customPermissionArray;
if (mCustomPermissionArray == null || mCustomPermissionArray.length == 0) {
return false;
}
isCustomPermissionOk = true;
mPermissionList.clear();
for (int i = 0; i < mCustomPermissionArray.length; i++) {
if (ContextCompat.checkSelfPermission(context, mCustomPermissionArray[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(mCustomPermissionArray[i]);
isCustomPermissionOk = false;
}
}
return isCustomPermissionOk;
}
/**
获取自定义权限
@param customPermissionArray 自定义权限数组
*/
public void getCustomPermission(Activity activity, String[] customPermissionArray) {
checkCustomPermission(activity, customPermissionArray);
if (mPermissionList.isEmpty()) {
//未授予的权限为空,表示都授予了
} else {
//请求权限方法
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将 List 转为数组
ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE_CUSTOM);
}
}
/**
获取自定义权限
@param customPermissionArray 自定义权限数组
*/
public void getCustomPermission(Fragment fragment, String[] customPermissionArray) {
checkCustomPermission(fragment.getContext(), customPermissionArray);
if (mPermissionList.isEmpty()) {
//未授予的权限为空,表示都授予了
} else {
//请求权限方法
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将 List 转为数组
fragment.requestPermissions(permissions, REQUEST_CODE_CUSTOM);
}
}
/**
检查日历权限
@return true 已获取日历权限,false 未获取日历权限
*/
public boolean checkCalendarPermission(Context context) {
int permission = ContextCompat.checkSelfPermission(context, PERMISSION_CALENDAR);
if (permission != PackageManager.PERMISSION_GRANTED) {
isCalendarPermissionOk = false;
} else {
isCalendarPermissionOk = true;
}
return isCalendarPermissionOk;
}
/**
获取日历权限-拒绝权限后仍然弹出权限窗
*/
public void getCalendarPermission1(Activity activity) {
if (!checkCalendarPermission(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_CALENDAR}, REQUEST_CODE_CALENDAR);
}
}
/**
获取日历权限-拒绝权限后仍然弹出权限窗
*/
public void getCalendarPermission1(Fragment fragment) {
if (!checkCalendarPermission(fragment.getContext())) {
fragment.requestPermissions(new String[]{PERMISSION_CALENDAR}, REQUEST_CODE_CALENDAR);
}
}
/**
获取日历权限-拒绝权限后不再弹出权限窗
shouldShowRequestPermissionRationale 方法,如果用户已经拒绝过则返回 true,即不再弹出权限窗
*/
public void getCalendarPermission2(Activity activity) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSION_CALENDAR)) {
// Show an expanation to the user asynchronously -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_CALENDAR}, REQUEST_CODE_CALENDAR);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
/**
获取日历权限-拒绝权限后不再弹出权限窗
shouldShowRequestPermissionRationale 方法,如果用户已经拒绝过则返回 true,即不再弹出权限窗
*/
public void getCalendarPermission2(Fragment fragment) {
// Should we show an explanation?
if (fragment.shouldShowRequestPermissionRationale(PERMISSION_CALENDAR)) {
// Show an expanation to the user asynchronously -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
fragment.requestPermissions(new String[]{PERMISSION_CALENDAR}, REQUEST_CODE_CALENDAR);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
/**
检查摄像头权限
@return true 已获取摄像头权限,false 未获取摄像头权限
*/
public boolean checkCameraPermission(Context context) {
int permission = ContextCompat.checkSelfPermission(context, PERMISSION_CAMERA);
if (permission != PackageManager.PERMISSION_GRANTED) {
isCameraPermissionOk = false;
} else {
isCameraPermissionOk = true;
}
return isCameraPermissionOk;
}
/**
获取摄像头权限-拒绝权限后仍然弹出权限窗
*/
public void getCameraPermission1(Activity activity) {
if (!checkCameraPermission(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_CAMERA}, REQUEST_CODE_CAMERA);
}
}
/**
获取摄像头权限-拒绝权限后仍然弹出权限窗
*/
public void getCameraPermission1(Fragment fragment) {
if (!checkCameraPermission(fragment.getContext())) {
fragment.requestPermissions(new String[]{PERMISSION_CAMERA}, REQUEST_CODE_CAMERA);
}
}
/**
获取摄像头权限-拒绝权限后不再弹出权限窗
*/
public void getCameraPermission2(Activity activity) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSION_CAMERA)) {
} else {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_CAMERA}, REQUEST_CODE_CAMERA);
}
}
/**
获取摄像头权限-拒绝权限后不再弹出权限窗
*/
public void getCameraPermission2(Fragment fragment) {
if (fragment.shouldShowRequestPermissionRationale(PERMISSION_CAMERA)) {
} else {
fragment.requestPermissions(new String[]{PERMISSION_CAMERA}, REQUEST_CODE_CAMERA);
}
}
/**
检查联系人权限
@return true 已获取联系人权限,false 未获取联系人权限
*/
public boolean checkContactsPermission(Context context) {
int permission = ContextCompat.checkSelfPermission(context, PERMISSION_CONTACTS);
if (permission != PackageManager.PERMISSION_GRANTED) {
isContactsPermissionOk = false;
} else {
isContactsPermissionOk = true;
}
return isContactsPermissionOk;
}
/**
获取联系人权限-拒绝权限后仍然弹出权限窗
*/
public void getContactsPermission1(Activity activity) {
if (!checkContactsPermission(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_CONTACTS}, REQUEST_CODE_CONTACTS);
}
}
/**
获取联系人权限-拒绝权限后仍然弹出权限窗
*/
public void getContactsPermission1(Fragment fragment) {
if (!checkContactsPermission(fragment.getContext())) {
fragment.requestPermissions(new String[]{PERMISSION_CONTACTS}, REQUEST_CODE_CONTACTS);
}
}
/**
获取联系人权限-拒绝权限后不再弹出权限窗
*/
public void getContactsPermission2(Activity activity) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSION_CONTACTS)) {
} else {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_CONTACTS}, REQUEST_CODE_CONTACTS);
}
}
/**
获取联系人权限-拒绝权限后不再弹出权限窗
*/
public void getContactsPermission2(Fragment fragment) {
if (fragment.shouldShowRequestPermissionRationale(PERMISSION_CONTACTS)) {
} else {
fragment.requestPermissions(new String[]{PERMISSION_CONTACTS}, REQUEST_CODE_CONTACTS);
}
}
/**
检查定位权限
@return true 已获取定位权限,false 未获取定位权限
*/
public boolean checkLocationPermission(Context context) {
int permission = ContextCompat.checkSelfPermission(context, PERMISSION_LOCATION);
if (permission != PackageManager.PERMISSION_GRANTED) {
isLocationPermissionOk = false;
} else {
isLocationPermissionOk = true;
}
return isLocationPermissionOk;
}
/**
获取定位权限-拒绝权限后仍然弹出权限窗
*/
public void getLocationPermission1(Activity activity) {
if (!checkLocationPermission(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_LOCATION}, REQUEST_CODE_LOCATION);
}
}
/**
获取定位权限-拒绝权限后仍然弹出权限窗
*/
public void getLocationPermission1(Fragment fragment) {
if (!checkLocationPermission(fragment.getContext())) {
fragment.requestPermissions(new String[]{PERMISSION_LOCATION}, REQUEST_CODE_LOCATION);
}
}
/**
获取定位权限-拒绝权限后不再弹出权限窗
*/
public void getLocationPermission2(Activity activity) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSION_LOCATION)) {
} else {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_LOCATION}, REQUEST_CODE_LOCATION);
}
}
/**
获取定位权限-拒绝权限后不再弹出权限窗
*/
public void getLocationPermission2(Fragment fragment) {
if (fragment.shouldShowRequestPermissionRationale(PERMISSION_LOCATION)) {
} else {
fragment.requestPermissions(new String[]{PERMISSION_LOCATION}, REQUEST_CODE_LOCATION);
}
}
/**
检查麦克风权限
@return true 已获取麦克风权限,false 未获取麦克风权限
*/
public boolean checkMicrophonePermission(Context context) {
int permission = ContextCompat.checkSelfPermission(context, PERMISSION_MICROPHONE);
if (permission != PackageManager.PERMISSION_GRANTED) {
isMicrophonePermissionOk = false;
} else {
isMicrophonePermissionOk = true;
}
return isMicrophonePermissionOk;
}
/**
获取麦克风权限-拒绝权限后仍然弹出权限窗
*/
public void getMicrophonePermission1(Activity activity) {
if (!checkMicrophonePermission(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_MICROPHONE}, REQUEST_CODE_MICROPHONE);
}
}
/**
获取麦克风权限-拒绝权限后仍然弹出权限窗
*/
public void getMicrophonePermission1(Fragment fragment) {
if (!checkMicrophonePermission(fragment.getContext())) {
fragment.requestPermissions(new String[]{PERMISSION_MICROPHONE}, REQUEST_CODE_MICROPHONE);
}
}
/**
获取麦克风权限-拒绝权限后不再弹出权限窗
*/
public void getMicrophonePermission2(Activity activity) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSION_MICROPHONE)) {
} else {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_MICROPHONE}, REQUEST_CODE_MICROPHONE);
}
}
/**
获取麦克风权限-拒绝权限后不再弹出权限窗
*/
public void getMicrophonePermission2(Fragment fragment) {
if (fragment.shouldShowRequestPermissionRationale(PERMISSION_MICROPHONE)) {
} else {
fragment.requestPermissions(new String[]{PERMISSION_MICROPHONE}, REQUEST_CODE_MICROPHONE);
}
}
/**
检查手机状态权限
@return true 已获取手机状态权限,false 未获取手机状态权限
*/
public boolean checkPhonePermission(Context context) {
int permission = ContextCompat.checkSelfPermission(context, PERMISSION_PHONE);
if (permission != PackageManager.PERMISSION_GRANTED) {
isPhonePermissionOk = false;
} else {
isPhonePermissionOk = true;
}
return isPhonePermissionOk;
}
/**
获取手机状态权限-拒绝权限后仍然弹出权限窗
*/
public void getPhonePermission1(Activity activity) {
if (!checkPhonePermission(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_PHONE}, REQUEST_CODE_PHONE);
}
}
/**
获取手机状态权限-拒绝权限后仍然弹出权限窗
*/
public void getPhonePermission1(Fragment fragment) {
if (!checkPhonePermission(fragment.getContext())) {
fragment.requestPermissions(new String[]{PERMISSION_PHONE}, REQUEST_CODE_PHONE);
}
}
/**
获取手机状态权限-拒绝权限后不再弹出权限窗
*/
public void getPhonePermission2(Activity activity) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSION_PHONE)) {
} else {
ActivityCompat.requestPermissions(activity, new String[]{PERMISSION_PHONE}, REQUEST_CODE_PHONE);
}
}
/**
获取手机状态权限-拒绝权限后不再弹出权限窗
*/
public void getPhonePermission2(Fragment fragment) {
if (fragment.shouldShowRequestPermissionRationale(PERMISSION_PHONE)) {
} else {
fragment.requestPermissions(new String[]{PERMISSION_PHONE}, REQUEST_CODE_PHONE);
}
}
/**
检查传感器权限
@return true 已获取传感器权限,false 未获取传感器权限
*/
public boolean checkSensorsPermission(Context context) {
int permission = ContextCompat.checkSelfPermission(context, PERMISSION_SENSORS);
if (permission != PackageManager.PERMISSION_GRANTED) {
isSensorsPermissionOk = false;
评论