android权限判断与申请封装类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android权限判断与申请封装类相关的知识,希望对你有一定的参考价值。


 

记录,将需要申请的权限封装到一个类中,判断或者申请某一权限只需传入对应权限即可。 

1、工具类代码:

public class PermissionUtils 
/**
* 检测权限
*
* @return true:已授权; false:未授权;
*/
public static boolean IscheckPermission(Context context, String permission)
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED)
return true;
else
return false;

/**
* 检测多个权限
*
* @return 未授权的权限
*/
public static List<String> IscheckMorePermissions(Context context, String[] permissions)
List<String> permissionList = new ArrayList<>();
for (int i = 0; i < permissions.length; i++)
if (!checkPermission(context, permissions[i]))
permissionList.add(permissions[i]);

return permissionList;

/**
* 请求权限
*/
public static void requestPermission(Context context, String permission, int requestCode)
ActivityCompat.requestPermissions((Activity) context, new String[]permission, requestCode);

/**
* 请求多个权限
*/
public static void requestMorePermissions(Context context, List permissionList, int requestCode)
String[] permissions = (String[]) permissionList.toArray(new String[permissionList.size()]);
requestMorePermissions(context, permissions, requestCode);

/**
* 请求多个权限
*/
public static void requestMorePermissions(Context context, String[] permissions, int requestCode)
ActivityCompat.requestPermissions((Activity) context, permissions, requestCode);

/**
* 判断是否已拒绝过权限
*
* @return
* @describe :如果应用之前请求过此权限但用户拒绝,此方法将返回 true;
* -----------如果应用第一次请求权限或 用户在过去拒绝了权限请求,
* -----------并在权限请求系统对话框中选择了 Dont ask again 选项,此方法将返回 false。
*/
public static boolean judgePermission(Context context, String permission)
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission))
return true;
else
return false;

/**
* 检测权限并请求权限:如果没有权限,则请求权限
*/
public static void IscheckAndRequestPermission(Context context, String permission, int requestCode)
if (!checkPermission(context, permission))
requestPermission(context, permission, requestCode);


/**
* 检测并请求多个权限
*/
public static void IscheckAndRequestMorePermissions(Context context, String[] permissions, int requestCode)
List<String> permissionList = checkMorePermissions(context, permissions);
requestMorePermissions(context, permissionList, requestCode);

/**
* 检测权限
*
* @describe:具体实现由回调接口决定
*/
public static void checkPermission(Context context, String permission, PermissionCheckCallBack callBack)
if (checkPermission(context, permission)) // 用户已授予权限
callBack.onHasPermission();
else
if (judgePermission(context, permission)) // 用户之前已拒绝过权限申请
callBack.onUserHasAlreadyTurnedDown(permission);
else // 用户之前已拒绝并勾选了不在询问、用户第一次申请权限。
callBack.onUserHasAlreadyTurnedDownAndDontAsk(permission);


/**
* 检测多个权限
*
* @describe:具体实现由回调接口决定
*/
public static void checkMorePermissions(Context context, String[] permissions, PermissionCheckCallBack callBack)
List<String> permissionList = checkMorePermissions(context, permissions);
if (permissionList.size() == 0) // 用户已授予权限
callBack.onHasPermission();
else
boolean isFirst = true;
for (int i = 0; i < permissionList.size(); i++)
String permission = permissionList.get(i);
if (judgePermission(context, permission))
isFirst = false;
break;


String[] unauthorizedMorePermissions = (String[]) permissionList.toArray(new String[permissionList.size()]);
if (isFirst)// 用户之前已拒绝过权限申请
callBack.onUserHasAlreadyTurnedDownAndDontAsk(unauthorizedMorePermissions);
else // 用户之前已拒绝并勾选了不在询问、用户第一次申请权限。
callBack.onUserHasAlreadyTurnedDown(unauthorizedMorePermissions);


/**
* 检测并申请权限
*/
public static void IscheckAndRequestPermission(Context context, String permission, int requestCode, PermissionRequestSuccessCallBack callBack)
if (checkPermission(context, permission)) // 用户已授予权限
callBack.onHasPermission();
else
requestPermission(context, permission, requestCode);


/**
* 检测并申请多个权限
*/
public static void IscheckAndRequestMorePermissions(Context context, String[] permissions, int requestCode, PermissionRequestSuccessCallBack callBack)
List<String> permissionList = checkMorePermissions(context, permissions);
if (permissionList.size() == 0) // 用户已授予权限
callBack.onHasPermission();
else
requestMorePermissions(context, permissionList, requestCode);


/**
* 判断权限是否申请成功
*/
public static boolean isPermissionRequestSuccess(int[] grantResults)
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
return true;
else
return false;

/**
* 用户申请权限返回
*/
public static void onRequestPermissionResult(Context context, String permission, int[] grantResults, PermissionCheckCallBack callback)
if (PermissionUtils.isPermissionRequestSuccess(grantResults))
callback.onHasPermission();
else
if (PermissionUtils.judgePermission(context, permission))
callback.onUserHasAlreadyTurnedDown(permission);
else
callback.onUserHasAlreadyTurnedDownAndDontAsk(permission);



/**
* 用户申请多个权限返回
*/
public static void onRequestMorePermissionsResult(Context context, String[] permissions, PermissionCheckCallBack callback)
boolean isBannedPermission = false;
List<String> permissionList = checkMorePermissions(context, permissions);
if (permissionList.size() == 0)
callback.onHasPermission();
else
for (int i = 0; i < permissionList.size(); i++)
if (!judgePermission(context, permissionList.get(i)))
isBannedPermission = true;
break;


// 已禁止再次询问权限
if (isBannedPermission)
callback.onUserHasAlreadyTurnedDownAndDontAsk(permissions);
else // 拒绝权限
callback.onUserHasAlreadyTurnedDown(permissions);


/**
* 跳转到权限设置界面
*/
public static void toAppSetting(Context context)
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9)
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
else if (Build.VERSION.SDK_INT <= 8)
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());

context.startActivity(intent);


2、权限状态结果的回调

public interface PermissionRequestSuccessCallBack 
/**
* 用户已授予权限
*/
void onHasPermission();

public interface PermissionCheckCallBack
/**
* 用户已授予权限
*/
void onHasPermission();
/**
* 用户已拒绝过权限
*
* @param permission:被拒绝的权限
*/
void onUserHasAlreadyTurnedDown(String... permission);
/**
* 用户已拒绝过并且已勾选不再询问选项、用户第一次申请权限;
*
* @param permission:被拒绝的权限
*/
void onUserHasAlreadyTurnedDownAndDontAsk(String... permission);

3、使用代码

PermissionUtils.checkMorePermissions(ViewPagerImgActivity.this, mPermissions, new PermissionUtils.PermissionCheckCallBack() 
@Override
public void onHasPermission()
ImageUtil.donwloadImg(ViewPagerImgActivity.this, url);//保存图片


@Override
public void onUserHasAlreadyTurnedDown(String... permission)
ToastUtil.showTextToas2(ViewPagerImgActivity.this, "用户已拒绝,设置中打开权限");


@Override
public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission)
ToastUtil.showTextToas2(ViewPagerImgActivity.this, "已拒绝,不再询问,设置中打开权限");

);

                                                                                  -END

以上是关于android权限判断与申请封装类的主要内容,如果未能解决你的问题,请参考以下文章

Android 动态权限申请 BaseActivity 封装 拨打电话

Android6.0权限申请工具类

Android 动态申请权限工具类

Android 动态申请权限工具类

Android ActivityResultContracts 请求权限(封装;含android 11权限变更)

Android ActivityResultContracts 请求权限(封装;含android 11权限变更)