很久没写过博客了,今天有空写一下.
1.在AndroidManifest里面配置需要的权限,无论是普通的还是特殊的权限都需要在这里配置。 2.检查权限,如果没有权限则申请 3.给用户申请权限的解释,这个方法只会在第一次用户拒绝授权,再次去申请这个的权限时会用到 4.申请相应的权限 5.处理申请权限的回调,在这里获得哪些授权成功哪些失败,处理需要的逻辑
EasyPermissions
compile 'pub.devrel:easypermissions:0.1.5' public class BaseActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{ //--------------------------------------权限封装------------------------------------------- private Map<Integer, PermissionCallback> mPermissonCallbacks; /** * 权限回调接口 */ protected interface PermissionCallback { /** * 成功获取权限 */ void haspermission(); } /** * 请求权限操作 * @param rationale 请求权限提示语 * @param permissionRequestCode 权限requestCode * @param perms 申请的权限列表 * @param callback 权限结果回调 */ protected void performCodeWithPermission(@NonNull String rationale, final int permissionRequestCode, @NonNull String[] perms, @NonNull PermissionCallback callback) { if (EasyPermissions.hasPermissions(this, perms)) { callback.hasPermission(); } else { if(mPermissonCallbacks == null){ mPermissonCallbacks = new HashMap<>(); } mPermissonCallbacks.put(permissionRequestCode, callback); EasyPermissions.requestPermissions(this, rationale, permissionRequestCode, perms); } } /** * 跳转设置弹框 建议在权限被设置为不在询问时弹出 提示用户前往设置页面打开权限 * @param tips 提示信息 */ protected void alertAppSetPermission(String tips) { new AppSettingsDialog.Builder(this, tips) .setTitle(getString(R.string.permission_deny_again_title)) .setPositiveButton(getString(R.string.permission_deny_again_positive)) .setNegativeButton(getString(R.string.permission_deny_again_nagative), null) .build() .show(); } /** * 跳转设置弹框 建议在权限被设置为不在询问时弹出 提示用户前往设置页面打开权限 * @param tips 提示信息 * @param requestCode 页面返回时onActivityResult的requestCode */ protected void alertAppSetPermission(String tips, int requestCode) { new AppSettingsDialog.Builder(this, tips) .setTitle(getString(R.string.permission_deny_again_title)) .setPositiveButton(getString(R.string.permission_deny_again_positive)) .setNegativeButton(getString(R.string.permission_deny_again_nagative), null) .setRequestCode(requestCode) .build() .show(); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); //这里会回调onPermissionsGranted 和 onPermissionsDenied EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } @Override public void onPermissionsGranted(int requestCode, List<String> perms) { PermissionCallback callback = mPermissonCallbacks.get(requestCode); if(callback != null) { callback.hasPermission(); } } @Override public void onPermissionsDenied(int requestCode, List<String> perms) { PermissionCallback callback = mPermissonCallbacks.get(requestCode); if(callback != null) { if(EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { alertAppSetPermission(getString(R.string.permission_tips), REQUEST_APPSET); } else { //普通取消(不授权也非不再询问) ,不做任何提示 } } }}新闻热点
疑难解答