public class PermissionManager {
public static final int PERMISSION_REQUEST = 100101;
public interface PermissionResultCallBack {
/**
* 授权成功
*/
void permissionSuccess();
/**
* 授权失败
*/
void permissionFail();
/**
* 应用管理设置
*/
void toSetPermission();
/**
* 标记不再提醒
*/
void noTipPermission();
}
/**
* 判断是否已经获取到权限
*
* @param permission
* @return
*/
public static boolean isHasPermission(String permission) {
return ContextCompat.checkSelfPermission(ApplicationManager.getContext(), permission) == PackageManager.PERMISSION_GRANTED;
}
public static void needPermission(final Activity context, final String permission) {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
// 第一次请求权限时,用户如果拒绝,下一次请求shouldShowRequestPermissionRationale()返回true
// 向用户解释需要这个权限
//if (shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE))
ActivityCompat.requestPermissions(context,
new String[]{permission}, PERMISSION_REQUEST);
}
}
public static void needPermission(final Activity context, final String[] permission) {
if (permission.length != 2)
throw new RuntimeException("同时申请权限个数固定为两个");
if (ContextCompat.checkSelfPermission(context, permission[0]) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(context, permission[1]) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, new String[]{permission[1]}, PERMISSION_REQUEST);
} else if (ContextCompat.checkSelfPermission(context, permission[0]) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(context, permission[1]) == PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, new String[]{permission[0]}, PERMISSION_REQUEST);
} else if (ContextCompat.checkSelfPermission(context, permission[0]) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(context, permission[1]) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, permission, PERMISSION_REQUEST);
}
}
public static void permissionResult(final Activity context, final String[] permissions, int[] grantResults, int requestCode, final PermissionResultCallBack resultCallBack) {
if (requestCode != PERMISSION_REQUEST||grantResults.length == 0)
return;
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
resultCallBack.permissionSuccess();
} else {
//用户勾选了不再询问
//提示用户手动打开权限
if (!ActivityCompat.shouldShowRequestPermissionRationale(context, permissions[0])) {
resultCallBack.noTipPermission();
if (!TextUtils.equals(permissions[0], Manifest.permission.ACCESS_FINE_LOCATION)) {
DialogFactory dialogFactory = new DialogFactory(context, "权限设置", getTips(permissions[0]), false, new DialogCallBack() {
@Override
public void titleRightOnClickListener() {
}
@Override
public void positiveOnClickListener() {
resultCallBack.toSetPermission();
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
context.startActivity(intent);
}
@Override
public void negativeOnClickListener() {
}
});
dialogFactory.setCancel(false);
dialogFactory.setNamePositiveButton("去开启");
dialogFactory.show();
}
} else {
resultCallBack.permissionFail();
}
}
}
private static String getTips(String permission) {
String tips = "";
switch (permission) {
case Manifest.permission.READ_PHONE_STATE:
tips = "开启获取手机信息权限后,大姨妈才能更好地为您服务哦~";
break;
case Manifest.permission.READ_EXTERNAL_STORAGE:
case Manifest.permission.WRITE_EXTERNAL_STORAGE:
tips = "开启存储权限后,大姨妈才能更好地为您服务哦~";
break;
case Manifest.permission.RECORD_AUDIO:
tips = "开启麦克风权限后,大姨妈才能更好地为您服务哦~";
break;
case Manifest.permission.ACCESS_FINE_LOCATION:
tips = "开启定位权限后,大姨妈才能更好地为您服务哦~";
break;
case Manifest.permission.CAMERA:
tips = "开启相机权限后,大姨妈才能更好地为您服务哦~";
break;
}
return tips;
}
}
Activity中添加回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionManager.permissionResult(this, permissions, grantResults, requestCode, new PermissionManager.PermissionResultCallBack() {
@Override
public void permissionSuccess() {
}
@Override
public void permissionFail() {
}
@Override
public void toSetPermission() {
}
@Override
public void noTipPermission() {
}
});
}