欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 动态检测开启 定位、读写权限

程序员文章站 2022-07-01 15:09:32
1、定义读写权限数组 /**读写权限 请求状态码*/ private static String[] PERMISSIONS_STORAGE = { android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE};2、动态加载权限方法 /** * 开启定位、读写权限...

静态权限:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- 文件读写权限  请检查手机应用是否被允许开启读写权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

1、定义读写权限数组

    /**读写权限 请求状态码*/
    private static String[] PERMISSIONS_STORAGE = {
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE};

2、动态加载权限方法

    /**
     * 开启定位、读写权限
     */
    private void openAccess() {
        //未开启定位权限
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
            //开启定位权限,200是标识码
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
        } else {
            //已开启定位
            /*ToastUtil.showToastSuccess(mContext, "已开启定位权限 !");*/
        }
//        开启 读写权限
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, 202);
            }else {
                //已开启 读写权限
                /*ToastUtil.showToastSuccess(mContext, "已开启读写权限 !");*/
            }
        }
    }

3、接收返回码,做出反应

   /**
     * @param requestCode   权限返回码
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 200:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    /*ToastUtil.showToastError(mContext, "未授予获取定位权限,应用退出 !");*/
                    ToastUtil.showToastError(mContext, "Obtain location permission is not granted, application exits!");
                    finish();
                }
                break;
            case 202:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    /*ToastUtil.showToastError(mContext, "未授予读写权限,应用退出 !");*/
                    ToastUtil.showToastError(mContext, "No read/write permission granted, application exits !");
                    finish();
                }
                break;
            default:
                break;
        }
    }

结束; 

 

 

本文地址:https://blog.csdn.net/qq_38629981/article/details/108727237