定位权限的获取方式

版权声明:本文为博主原创文章,转载请标明原文出处链接。 https://blog.csdn.net/iblade/article/details/86182798
/**
 * 与权限相关的工具类
 * Created by @author iblade.Wang on 2019/1/8 19:22
 */
public class PermissionUtils {


    /**
     * 判断位置权限集合
     */
    public static String[] permissionsLocation = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    public static final int OP_FINE_LOCATION = 1;

    /**
     * 判断位置权限
     *
     * @param mContext 权限数组
     * @return true-表示没有位置权限  false-表示权限已开启
     */
    public static boolean lacksPermissionsLocation(Context mContext) {
        int result = checkOp(mContext, OP_FINE_LOCATION, AppOpsManager.OPSTR_FINE_LOCATION);
        return result == AppOpsManagerCompat.MODE_IGNORED;
    }

    /**
     * 判断位置权限
     *
     * @param mContext 权限数组
     * @return true-表示没有位置权限  false-表示权限已开启
     */
    public static boolean lacksPermissionsLocation2(Context mContext) {
        for (String permission : permissionsLocation) {
            if (lacksPermission(mContext, permission)) {
                return true;
            }
        }
        return false;

    }

    /**
     * 判断是否缺少权限
     *
     * @param context    上下文
     * @param permission 权限
     * @return 是否已关闭该权限,true-已关闭,false-已开启
     */
    private static boolean lacksPermission(Context context, String permission) {
        if (context.getApplicationInfo().targetSdkVersion >= 23) {
            return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED;
        } else {
            return PermissionChecker.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED;
        }
    }

    /**
     * 检查权限列表
     *
     * @param context
     * @param op       这个值被hide了,去AppOpsManager类源码找,如位置权限  AppOpsManager.OP_GPS==2
     * @param opString 如判断定位权限 AppOpsManager.OPSTR_FINE_LOCATION
     * @return @see 如果返回值 AppOpsManagerCompat.MODE_IGNORED 表示被禁用了
     */
    public static int checkOp(Context context, int op, String opString) {
        final int version = Build.VERSION.SDK_INT;
        if (version >= 19) {
            Object object = context.getSystemService(Context.APP_OPS_SERVICE);
            Class c = object.getClass();
            try {
                Class[] cArg = new Class[3];
                cArg[0] = int.class;
                cArg[1] = int.class;
                cArg[2] = String.class;
                Method lMethod = c.getDeclaredMethod("checkOp", cArg);
                return (Integer) lMethod.invoke(object, op, Binder.getCallingUid(), context.getPackageName());
            } catch (Exception e) {
                e.printStackTrace();
                if (Build.VERSION.SDK_INT >= 23) {
                    return AppOpsManagerCompat.noteOp(context, opString, context.getApplicationInfo().uid,
                            context.getPackageName());
                }

            }
        }
        return -1;
    }


    /**
     * 跳转至AppDetailSetting,用于开启权限
     *
     * @param context 上下文
     * @return 跳转intent
     */
    public static Intent getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // SDK >= 9
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else /*if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) */ {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        return localIntent;
    }
}

展开阅读全文

没有更多推荐了,返回首页