package com.example.gaowen38.utils; 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 androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import com.example.gaowen38.AppConstant; import com.example.gaowen38.Base.BaseDialog; import com.example.gaowen38.Base.MyBaseActivity; import java.util.ArrayList; import java.util.List; import cn.view.viewlibrary.utils.Lg; import cn.view.viewlibrary.utils.StringUtils; public class PermissionUtils { public final static int REQUEST_CODE_ASK_PERMISSIONS = 54007; //默认运行权限 private static String[] permissionArray = new String[]{ Manifest.permission.WRITE_SETTINGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION }; //必须要开启的权限列表 private static List<String> needRequestPermissionList = new ArrayList<>(); /** * 判断所有权限是否已开启 * * @param activity * @return */ public static boolean isOpenAllPermission(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { getNeedPermission(activity); if (needRequestPermissionList != null && needRequestPermissionList.size() == 0) { return true; } } else { return true; } BaseDialog.showSimpleDialog(activity, "提醒", "请先开启权限", "确定", "取消", new BaseDialog.OnClickListener() { @Override public void onDialogClick(int buttonID) { if (buttonID == BaseDialog.ID_OK) { startAppPermissionSettings(activity); } } }); return false; } /** * 获取必须要注册的权限 * * @param activity */ private static void getNeedPermission(Activity activity) { if (needRequestPermissionList == null) { needRequestPermissionList = new ArrayList<>(); } needRequestPermissionList.clear(); for (int i = 0; i < permissionArray.length; i++) { if (ContextCompat.checkSelfPermission(activity, permissionArray[i]) != PackageManager.PERMISSION_GRANTED) { needRequestPermissionList.add(permissionArray[i]); } } } /** * 请求权限 */ public static void requestPermission(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { for (int i = 0; i < permissionArray.length; i++) { if (ContextCompat.checkSelfPermission(activity, permissionArray[i]) != PackageManager.PERMISSION_GRANTED) { Lg.d(StringUtils.concat("当前开启权限:", permissionArray[i])); if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionArray[i])) { //android 6.0权限 大于23时判断,其他的都是8.0权限,大于26判断 // if (permissionArray[i].equals(Manifest.permission.WRITE_SETTINGS) ) { // if ( !Settings.System.canWrite(activity)) { // Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); // intent.setData(Uri.parse("package:" + DeviceUtils.getAppPackageName())); // activity.startActivity(intent); // } // } else { ActivityCompat.requestPermissions(activity, permissionArray, REQUEST_CODE_ASK_PERMISSIONS); // } break; } } } } } /** * 请求安装未知应用权限 (android 8.0新特性) */ public static boolean requestInstallPermission(Activity activity) { boolean haveInstallPermission = true; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //先获取是否有安装未知来源应用的权限 haveInstallPermission = activity.getPackageManager().canRequestPackageInstalls(); if (!haveInstallPermission) { //没有权限 Uri packageURI = Uri.parse("package:" + DeviceUtils.getAppPackageName()); //注意这个是8.0新API Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI); ((MyBaseActivity) activity).startActivityForResult(intent, AppConstant.m_permission_code_100); } } return haveInstallPermission; } /** * 启动当前应用的权限设置页面 * * @param activity */ private static void startAppPermissionSettings(Activity activity) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + activity.getPackageName())); activity.startActivity(intent); } }
Android动态权限申请工具类
于 2019-10-31 11:24:21 首次发布