android 权限请求
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
public class AndPermissionUtil {
private static final int REQ_PERMISSION_CODE = 0x100;
private static Map<String, String> mMap = new HashMap<>();
//权限检查 Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE
public static boolean checkPermission(Activity activity, String... permissions) {
if (permissions == null || permissions.length == 0) {
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<String> perList = new ArrayList<>();
for (String permission : permissions) {
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, permission)) {
perList.add(permission);
if (StrUtil.isEmpty(mMap.get(permission))) {
mMap.put(permission, "0");
} else {
mMap.put(permission, "1");
}
}
}
if (perList.size() != 0) {
boolean isShouldShowRequest = false;
String[] permissionsArray = perList.toArray(new String[1]);
for (String value : mMap.values()) {
// 判断是否第一次请求权限
if (Integer.parseInt(value) == 0) {
isShouldShowRequest = true;
break;
}
}
// 第一次请求权限,不判断shouldShowRequestPermissionRationale ,直接调用requestPermissions
if (isShouldShowRequest) {
ActivityCompat.requestPermissions(activity, permissionsArray, REQ_PERMISSION_CODE);
} else {
// shouldShowRequestPermissionRationale,判断是否勾选不在提醒,勾选返回false
List<String> tempList = new ArrayList<>();
for (String permission : perList) {
boolean b = ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
if (b) {
tempList.add(permission);
}
}
if (tempList.size() > 0) {
ActivityCompat.requestPermissions(activity, permissionsArray, REQ_PERMISSION_CODE);
} else {
defaultApi(activity, perList);
}
}
return false;
}
}
mMap.clear();
return true;
}
public static boolean onRequestPermissionsResult(Activity activity, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults, PermissionCallback callback) {
if (requestCode == REQ_PERMISSION_CODE) {
if (verifyPermissions(grantResults)) {
//有权限了
Log.d("PermissionsResult", "hasPermission");
if (callback != null) {
callback.hasPermission();
}
} else {
Log.d("PermissionsResult", "noPermission");
// checkPermission(activity, permissions);
ToastUtils.showShortToast("权限未开启,此功能将不可用");
}
return false;
}
return true;
}
private static boolean verifyPermissions(int[] grantResults) {
// At least one result must be checked.
if (grantResults.length < 1) {
return false;
}
// Verify that each required permission has been granted, otherwise return false.
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private static void defaultApi(Activity context, List<String> message) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
new AlertDialog.Builder(context, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert).setCancelable(false)
.setMessage("请开启" + getPermissionName(message) + "权限.")
.setNegativeButton("残忍拒绝", (dialog, which) -> dialog.dismiss())
.setPositiveButton("去添加权限", (dialog, which) -> {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
context.startActivityForResult(intent, 0x002);
})
.show();
}
}
public interface PermissionCallback {
void hasPermission();
}
private static String getPermissionName(List<String> perList) {
StringBuilder sb = new StringBuilder();
for (String s : perList) {
switch (s) {
case Manifest.permission.WRITE_EXTERNAL_STORAGE:
case Manifest.permission.READ_EXTERNAL_STORAGE:
sb.append("存储");
break;
case Manifest.permission.RECORD_AUDIO:
sb.append("麦克风");
break;
case Manifest.permission.CAMERA:
sb.append("相机");
break;
case Manifest.permission.READ_PHONE_STATE:
case Manifest.permission.CALL_PHONE:
case Manifest.permission.READ_CALL_LOG:
case Manifest.permission.WRITE_CALL_LOG:
case Manifest.permission.USE_SIP:
case Manifest.permission.PROCESS_OUTGOING_CALLS:
case Manifest.permission.ADD_VOICEMAIL:
sb.append("电话");
break;
}
}
return sb.toString();
}
}
权限请求
if (AndPermissionUtil.checkPermission(this, mPermissions)) {
....
}
Activity中重写onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean result = AndPermissionUtil.onRequestPermissionsResult(this, requestCode, permissions,
grantResults, () -> {
....
});
if (result) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}