安卓权限动态加载之权限管理类
package com.zzh.hang_up_phone_auto;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class PermissionManager {
public static final int PHONE_REQUEST_CODE = 1;
private Activity activity;
private String[] permissions;
private int requestCode;
public PermissionManager(Activity activity, String[] permissions, int requestCode) {
this.activity = activity;
this.permissions = permissions;
this.requestCode = requestCode;
}
public void requestForAccess() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean[] ifAgreePermission = ifAgreePermission();
for (int i = 0; i < permissions.length; i++) {
if (!ifAgreePermission[i]) {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
}
}
}
}
public boolean[] ifAgreePermission() {
boolean[] result = new boolean[permissions.length];
for (int i = 0; i < permissions.length; i++) {
result[i] = (ContextCompat.checkSelfPermission(activity, permissions[i])
== PackageManager.PERMISSION_GRANTED);
}
return result;
}
}