6.0Android 开发技术交流Android开发进阶

Android6.0 权限管理解决方案 PermissionDi

2017-05-26  本文已影响91人  大数据修行

1. 6.0权限的特点

6.0以前是安装时提醒你需要什么权限,这样有的权限你不想给,但要安装就一定要给.
Android6.0在AndroidManifest当中配置了权限之后,安装时不会一次确认你需要什么权限,而是在你需要权限的时候再提醒你需要的危险权限,再在这里进行处理.

2. 危险权限

这些权限是等到需要的时候要提示的

CALENDAR

3.PermissionDispatcher的应用

PermissionDispatcher GitHub链接
这个库可以通过注解的方式快速生产处理权限的代码,非常方便.
1.加入引用依赖
在app module build.gradle下加入

dependencies {
  compile('com.github.hotchemi:permissionsdispatcher:${latest.version}') {
      // if you don't use android.app.Fragment you can exclude support for them
      exclude module: "support-v13"
  }
  annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:${latest.version}'
}```
**2.在manifest中申请权限**
如下
><uses-permission android:name="android.permission.CAMERA" />

**3.在要申请权限的类和方法上面加上注解**
有几个注解要加

注解方法要声明为private
Annotation                Required          Description
>* @RuntimePermissions
     ✓  
Register an Activity or Fragment(we support both) to handle permissions
在Activity或者Fragment 类前面加上@RuntimePermissions注解 **必须加**
* @NeedsPermission  
✓   
Annotate a method which performs the action that requires one or more permissions
在需要权限的方法前面加上@NeedsPermissions注解 **必须加**
* @OnShowRationale  
    Annotate a method which explains why the permission/s is/are needed. It passes in a PermissionRequest object which can be used to continue or abort the current permission request upon user input
* @OnPermissionDenied   
    Annotate a method which is invoked if the user doesn't grant the permissions
权限被拒接后的方法 加上@OnPermissionDenied注解
* @OnNeverAskAgain  
    Annotate a method which is invoked if the user chose to have the device "never ask again" about a permission

**注解后面加上需要的权限
如下**

@RuntimePermissions public class CreateVisitActivity extends BaseTitleActivity

@NeedsPermission({
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_CALENDAR,
Manifest.permission.READ_CALENDAR
}) void toModifyVisit() {
createVisitPresenter.modifyVisit();
}

/**
* 如果用户拒绝该权限执行的方法
*/
@OnPermissionDenied({
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_CALENDAR,
Manifest.permission.READ_CALENDAR
}) void onPermissionDenied() {
PermissionUtils.showMissingPermissionDialog(this);
}</br>

**4.build一下,就会自动生成一个类名为XXXActivityPermissionDispatcher类**
如下

package com.pingan.bank.enrich.module.jiaoliu.createVisit;

import android.support.v4.app.ActivityCompat;
import java.lang.String;
import permissions.dispatcher.PermissionUtils;

final class CreateVisitActivityPermissionsDispatcher {
private static final int REQUEST_TOMODIFYVISIT = 1;

private static final String[] PERMISSION_TOMODIFYVISIT = new String[] {"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.WRITE_CALENDAR","android.permission.READ_CALENDAR"};

private static final int REQUEST_TOCREATEVISIT = 2;

private static final String[] PERMISSION_TOCREATEVISIT = new String[] {"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.WRITE_CALENDAR","android.permission.READ_CALENDAR"};

private CreateVisitActivityPermissionsDispatcher() {
}

static void toModifyVisitWithCheck(CreateVisitActivity target) {
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_TOMODIFYVISIT)) {
target.toModifyVisit();
} else {
ActivityCompat.requestPermissions(target, PERMISSION_TOMODIFYVISIT, REQUEST_TOMODIFYVISIT);
}
}

static void toCreateVisitWithCheck(CreateVisitActivity target) {
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_TOCREATEVISIT)) {
target.toCreateVisit();
} else {
ActivityCompat.requestPermissions(target, PERMISSION_TOCREATEVISIT, REQUEST_TOCREATEVISIT);
}
}

static void onRequestPermissionsResult(CreateVisitActivity target, int requestCode, int[] grantResults) {
switch (requestCode) {
case REQUEST_TOMODIFYVISIT:
if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_TOMODIFYVISIT)) {
target.onPermissionDenied();
return;
}
if (PermissionUtils.verifyPermissions(grantResults)) {
target.toModifyVisit();
} else {
target.onPermissionDenied();
}
break;
case REQUEST_TOCREATEVISIT:
if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_TOCREATEVISIT)) {
target.onPermissionDenied();
return;
}
if (PermissionUtils.verifyPermissions(grantResults)) {
target.toCreateVisit();
} else {
target.onPermissionDenied();
}
break;
default:
break;
}
}
}

再在XXXActivity里面加上请求成功失败的回调方法

/**
* 权限请求回调,提示用户之后,用户点击“允许”或者“拒绝”之后调用此方法
*
* @param requestCode 定义的权限编码
* @param permissions 权限名称
* @param grantResults 允许/拒绝
*/
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
CreateVisitActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode,
grantResults);
}

5.在需要权限的地方调用XXXActivityPermissionDispatcher的方法
如下

tvConcern.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (etVisitTitle.getText().toString().length() >= 4
//&& !TextUtils.isEmpty(etVisitReason.getText().toString())
&& tvStartTime.getText().toString() != ""
&& tvStopTime.getText().toString() != ""
&& !TextUtils.isEmpty(tvJoinMember.getText().toString())) {
if (isModify()) {
CreateVisitActivityPermissionsDispatcher.toModifyVisitWithCheck(
CreateVisitActivity.this);
} else {
CreateVisitActivityPermissionsDispatcher.toCreateVisitWithCheck(
CreateVisitActivity.this);
}
}
}
});

##有任何疑问可以在评论区和我交流,喜欢的点个👍鼓励下吧
上一篇下一篇

猜你喜欢

热点阅读