首先我认为看这篇文章的人都应该是会用官方的请求方式的,那么就请先看以下代码,如果觉得有兴趣,再继续往下看:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//直接把上面三个参数写进来就行了
AntiPermissionUtils.getInstance().handlePermission( requestCode, permissions, grantResults );
super.onRequestPermissionsResult( requestCode, permissions, grantResults );
}
AntiPermissionUtils.getInstance().requestPermission( this,
new AntiPermission( Manifest.permission.SEND_SMS, new AntiPermissionListener() {
@Override
public void success() {
//允许
}
@Override
public void failed() {
//不允许
}
@Override
public void refuse() {
//不再提示
}
} ));
AntiPermissionUtils.getInstance().requestPermission( this,
new AntiPermission( Manifest.permission.SEND_SMS, new AntiPermissionListener() {
@Override
public void success() {
//允许
}
@Override
public void failed() {
//不允许
}
@Override
public void refuse() {
//不再提示
}
} )
,new AntiPermission( Manifest.permission.CAMERA, new AntiPermissionListener() {
@Override
public void success() {
//允许
}
@Override
public void failed() {
//不允许
}
@Override
public void refuse() {
//不再提示
}
} )
,new AntiPermission( Manifest.permission.CALL_PHONE, new AntiPermissionListener() {
@Override
public void success() {
//允许
}
@Override
public void failed() {
//不允许
}
@Override
public void refuse() {
//不再提示
}
} ));
如果觉得这样写的方式还比较简洁好用的话,那么就继续往下看咯。
在Build.VERSION.SDK_INT < 23的时候是不用判断权限的
在已经授权这里需要用ContextCompat.checkSelfPermission(Context context, String permission)这个方法来判断的,在判断成功之后不再进行下一步
在上一步判断未授权之后调用请求授权的方法ActivityCompat.requestPermissions(Activity activity, String[] permissions, int requestCode)来请求授权
在onRequestPermissionsResult中判断是否授权和是否不再提示
上面说到的那么多东西里面只有activity和permission是都有的,requestCode可以通过permission的hashcode来生成一个。然后还需要做回调,那么还需要一个interface来封装三个回调的操作。
public interface AntiPermissionListener {
/**
* 成功
*/
void success();
/**
* 失败
*/
void failed();
/**
* 用户选择了不再提示
*/
void refuse();
}
需要把permission和interface一一对应的保存起来
public class AntiPermission {
public AntiPermission(String permissionName, AntiPermissionListener listener) {
this.permissionName = permissionName;
this.listener = listener;
}
String permissionName;
AntiPermissionListener listener;
}
public class AntiPermissionUtils {
private Map<Integer, Map<String, AntiPermissionListener>> map = new HashMap<>();
private Map<Integer, Activity> activitys = new HashMap<>();
private static AntiPermissionUtils instance;
private AntiPermissionUtils() {
}
public static AntiPermissionUtils getInstance() {
if (instance == null) {
synchronized (AntiPermission.class) {
if (instance == null) {
instance = new AntiPermissionUtils();
}
}
}
return instance;
}
public void requestPermission(Activity activity, AntiPermission... antiPermissions) {
if (Build.VERSION.SDK_INT < 23) {
for (AntiPermission antiPermission : antiPermissions) {
antiPermission.listener.success();
}
return;
}
Map<String, AntiPermissionListener> sa = new HashMap<>();
for (AntiPermission antiPermission : antiPermissions) {
if (ContextCompat.checkSelfPermission(activity, antiPermission.permissionName) != PackageManager.PERMISSION_GRANTED) {
sa.put(antiPermission.permissionName, antiPermission.listener);
} else {
antiPermission.listener.success();
}
}
if (sa.size() == 0) {
return;
}
int code = activitys.hashCode();
code = code % 1000 + code / 100000;
activitys.put(code, activity);
map.put(code, sa);
String[] permissionNames = new String[sa.size()];
Iterator iterator = sa.keySet().iterator();
int index = 0;
while (iterator.hasNext() && index < permissionNames.length) {
permissionNames[index] = (String) iterator.next();
index++;
}
ActivityCompat.requestPermissions(activity, permissionNames,
code);
}
public void handlePermission(int requestCode, String[] permissions, int[] grantResults) {
if (permissions == null || grantResults == null || permissions.length == 0 || grantResults.length == 0) {
AntiLog.e(AntiPermissionUtils.class.getSimpleName(), "some arguments is null!");
return;
}
if (!map.containsKey(requestCode)) {
AntiLog.e(AntiPermissionUtils.class.getSimpleName(), "Permission is not registed!");
return;
}
Map<String, AntiPermissionListener> antiPermissions = map.get(requestCode);
Activity activity = activitys.get(requestCode);
for (int i = 0; i < permissions.length; i++) {
boolean isTip = ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[i]);
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
antiPermissions.get(permissions[i]).success();
} else {
if (isTip) {
antiPermissions.get(permissions[i]).failed();
} else {
antiPermissions.get(permissions[i]).refuse();
}
}
}
map.remove(antiPermissions);
activitys.remove(activity);
}
}
这里操作都比较简单,就是循环之类的,就不写注释了,能够很轻松的看懂的,然后你就能够简单快捷的进行权限请求了,这种一个项目中用几次的东西,不能浪费太多的时间在上面呀。如果你还想更方便的话,可以再看看我这个library:,能够更方便快速的让你开发一个APP。
转载请注明出处: