public class PermissionHelper { public interface OnPermissionCallback { /** * * @param success * @return true handled false not handle */ public boolean onPermissionResult(boolean success); } private Context mContext; private final String PACKAGE = "package:"; private final int REQUEST_CODE = 12; private OnPermissionCallback mOnPermissionCallback; public PermissionHelper(Context context, OnPermissionCallback onPermissionCallback) { if(context == null || onPermissionCallback == null) { throw new IllegalArgumentException(); } else { mContext = context; mOnPermissionCallback = onPermissionCallback; } } /** * 判断权限集合 * * @param permissions 检测权限的集合 * @return List 没有获取权限的列表 */ public List<String> checkPermissions(String... permissions) { List<String> permissionsList = new ArrayList<String>(); for (String permission : permissions) { if (!checkPermission(permission)) { permissionsList.add(permission); } } if (permissionsList != null && permissionsList.size() > 0){ requestPermissions(permissionsList, REQUEST_CODE); }else{ if(mOnPermissionCallback != null) { mOnPermissionCallback.onPermissionResult(true); } } return permissionsList; } /** * 判断权限是否获取 * * @param permission 权限名称 * @return 已受权返回true,未受权返回false */ public boolean checkPermission(String permission) { return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_GRANTED; } /** * 获取权限 * * @param resultCode * @return */ public void requestPermissions(List<String> permissionsList, int resultCode) { if(permissionsList == null || permissionsList.isEmpty()) return; List<String> permissionsNeeded = new ArrayList<String>(); for (String permission : permissionsList) { // 注意这里要使用shouldShowRequestPermissionRationale而不要使用requestPermission方法 // 由于requestPermissions方法会显示不在询问按钮 if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext, permission)) { permissionsNeeded.add(permission); } } if (permissionsNeeded.size() > 0) { //若是用户之前拒绝过改权限申请,则给用户提示 showMissingPermissionDialog(); } else { //进行权限请求 ActivityCompat.requestPermissions((Activity) mContext, permissionsList.toArray(new String[permissionsList.size()]), resultCode); } } public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch(requestCode){ case REQUEST_CODE: boolean success = false; if(permissions != null && permissions.length > 0 && grantResults != null && grantResults.length > 0) { for (int i = 0; i < grantResults.length; i++) { success = grantResults[i] == PackageManager.PERMISSION_GRANTED; if(!success) break; } } boolean handle = false; if(mOnPermissionCallback != null) { handle = mOnPermissionCallback.onPermissionResult(success); } if (!success && !handle) { startAppSettings(); } break; } } // 显示缺失权限提示 private void showMissingPermissionDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); final AlertDialog alertDialog = builder.create(); builder.setMessage("当前应用缺乏必要权限。\n\n请点击设置按钮,进入应用信息界面打开所需权限。"); // 拒绝, 退出应用 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); if(mOnPermissionCallback != null) { mOnPermissionCallback.onPermissionResult(false); } } }); builder.setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); startAppSettings(); } }); builder.show(); } // 启动应用的设置 public void startAppSettings() { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse( PACKAGE + mContext.getPackageName())); mContext.startActivity(intent); } } //引用代码 private PermissionHelper mPermissionHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPermissionHelper = new PermissionHelper(this, new PermissionHelper.OnPermissionCallback() { @Override public boolean onPermissionResult(boolean success) { if(success) { doSuccess(); } else { finish(); } return true; } }); } @Override protected void onResume() { super.onResume(); //判断权限受权状态 mPermissionHelper.checkPermissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); mPermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults); }
以上代码再这两篇文章基础上修改: Android 6.0RunTime Permission完美解决方案ide