Android一个简单易用的Android6.0权限工具类

一.概述

最近因为工作需要用到Android6.0权限,就在网上找了下6.0权限的相关,发现了一篇好文章 ,请戳[小爱_小世界 ]的这篇文章,文章写的很好,解决 了我的问题及疑惑,而我的这个就是基于这篇文章的,可以说基本差不多,只是将使用方法稍微总结了一下,也算是自己的一个学习笔记!!!

二.运行时权限的变化及特点

对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要访问通讯录、短信等)。而在6.0以后,我们可以直接安装,当app需要我们授予不恰当的权限的时候,我们可以予以拒绝(比如:单机的象棋对战,请求访问任何权限,我都是不同意的)。当然你也可以在设置界面对每个app的权限进行查看,以及对单个权限进行授权或者解除授权。
新的权限机制更好的保护了用户的隐私,Google将权限分为两类,一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。

  • Normal Permissions如下:
  android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
  android.permission.ACCESS_NETWORK_STATE
  android.permission.ACCESS_NOTIFICATION_POLICY
  android.permission.ACCESS_WIFI_STATE
  android.permission.ACCESS_WIMAX_STATE
  android.permission.BLUETOOTH
  android.permission.BLUETOOTH_ADMIN
  android.permission.BROADCAST_STICKY
  android.permission.CHANGE_NETWORK_STATE
  android.permission.CHANGE_WIFI_MULTICAST_STATE
  android.permission.CHANGE_WIFI_STATE
  android.permission.CHANGE_WIMAX_STATE
  android.permission.DISABLE_KEYGUARD
  android.permission.EXPAND_STATUS_BAR
  android.permission.FLASHLIGHT
  android.permission.GET_ACCOUNTS
  android.permission.GET_PACKAGE_SIZE
  android.permission.INTERNET
  android.permission.KILL_BACKGROUND_PROCESSES
  android.permission.MODIFY_AUDIO_SETTINGS
  android.permission.NFC
  android.permission.READ_SYNC_SETTINGS
  android.permission.READ_SYNC_STATS
  android.permission.RECEIVE_BOOT_COMPLETED
  android.permission.REORDER_TASKS
  android.permission.REQUEST_INSTALL_PACKAGES
  android.permission.SET_TIME_ZONE
  android.permission.SET_WALLPAPER
  android.permission.SET_WALLPAPER_HINTS
  android.permission.SUBSCRIBED_FEEDS_READ
  android.permission.TRANSMIT_IR
  android.permission.USE_FINGERPRINT
  android.permission.VIBRATE
  android.permission.WAKE_LOCK
  android.permission.WRITE_SYNC_SETTINGS
  com.android.alarm.permission.SET_ALARM
  com.android.launcher.permission.INSTALL_SHORTCUT
  com.android.launcher.permission.UNINSTALL_SHORTCUT
  • 6.0危险权限如下(只要使用了一组中的一个,该组中的其他权限默认可用):
 group:android.permission-group.CONTACTS
    permission:android.permission.WRITE_CONTACTS
    permission:android.permission.GET_ACCOUNTS    
    permission:android.permission.READ_CONTACTS

  group:android.permission-group.PHONE
    permission:android.permission.READ_CALL_LOG
    permission:android.permission.READ_PHONE_STATE 
    permission:android.permission.CALL_PHONE
    permission:android.permission.WRITE_CALL_LOG
    permission:android.permission.USE_SIP
    permission:android.permission.PROCESS_OUTGOING_CALLS
    permission:com.android.voicemail.permission.ADD_VOICEMAIL

  group:android.permission-group.CALENDAR
    permission:android.permission.READ_CALENDAR
    permission:android.permission.WRITE_CALENDAR

  group:android.permission-group.CAMERA
    permission:android.permission.CAMERA

  group:android.permission-group.SENSORS
    permission:android.permission.BODY_SENSORS

  group:android.permission-group.LOCATION
    permission:android.permission.ACCESS_FINE_LOCATION
    permission:android.permission.ACCESS_COARSE_LOCATION

  group:android.permission-group.STORAGE
    permission:android.permission.READ_EXTERNAL_STORAGE
    permission:android.permission.WRITE_EXTERNAL_STORAGE

  group:android.permission-group.MICROPHONE
    permission:android.permission.RECORD_AUDIO

  group:android.permission-group.SMS
    permission:android.permission.READ_SMS
    permission:android.permission.RECEIVE_WAP_PUSH
    permission:android.permission.RECEIVE_MMS
    permission:android.permission.RECEIVE_SMS
    permission:android.permission.SEND_SMS
    permission:android.permission.READ_CELL_BROADCASTS

三.权限工具类

public class PermissionUtils {

private static final String TAG = PermissionUtils.class.getSimpleName();

public static final int CODE_CONTACTS = 0;//联系人
public static final int CODE_PHONE = 1;//电话
public static final int CODE_CALENDAR = 2;//日历
public static final int CODE_CAMERA = 3;//相机
public static final int CODE_SENSORS = 4;//传感器
public static final int CODE_LOCATION = 5;//定位
public static final int CODE_STORAGE = 6;//内存卡
public static final int CODE_MICROPHONE = 7;//麦克风
public static final int CODE_SMS = 8;//短信

public static final int CODE_MULTI_PERMISSION = 100;//多个权限

//以下是对应上面的组权限,只需要申请一个组内的权限即可
public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
public static final String PERMISSION_READ_CALENDAR = Manifest.permission.READ_CALENDAR;
public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
public static final String PERMISSION_BODY_SENSORS = Manifest.permission.BODY_SENSORS;
public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
public static final String PERMISSION_READ_SMS = Manifest.permission.READ_SMS;

private static final String[] requestPermissions = {
        PERMISSION_GET_ACCOUNTS,
        PERMISSION_CALL_PHONE,
        PERMISSION_READ_CALENDAR,
        PERMISSION_CAMERA,
        PERMISSION_BODY_SENSORS,
        PERMISSION_ACCESS_FINE_LOCATION,
        PERMISSION_READ_EXTERNAL_STORAGE,
        PERMISSION_RECORD_AUDIO,
        PERMISSION_READ_SMS,
};


/**
 * 请求回调接口
 */
public interface PermissionGrant {
    void onPermissionGranted(int requestCode);
}

/**
 * 请求 permission.
 *
 * @param activity    上下文
 * @param requestCode request code 如果需要Camera权限,就使用PermissionUtils.CODE_CAMERA
 */
public static void requestPermission(final Activity activity, final int requestCode, PermissionGrant permissionGrant) {
    if (activity == null) {
        return;
    }
    if (requestCode < 0 || requestCode >= requestPermissions.length) {
        Log.w(TAG, "requestPermission illegal requestCode:" + requestCode);
        return;
    }


    //如果是6.0以下的手机,ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED,
    // 但是,如果用户关闭了你申请的权限,ActivityCompat.checkSelfPermission(),会导致程序崩溃(java.lang.RuntimeException: Unknown exception code: 1 msg null),
    // 你可以使用try{}catch(){},处理异常,也可以判断系统版本,低于23就不申请权限,直接做你想做的。permissionGrant.onPermissionGranted(requestCode);
//        if (Build.VERSION.SDK_INT < 23) {
//            permissionGrant.onPermissionGranted(requestCode);
//            return;
//        }

    final String requestPermission = requestPermissions[requestCode];
    int checkSelfPermission;
    try {
        checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
    } catch (RuntimeException e) {
        Toast.makeText(activity, activity.getResources().getText(R.string.Toast_permission), Toast.LENGTH_SHORT).show();
        return;
    }

    if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");

        //判断是否需要解释弹窗
        // 第一次 false 要允许吗(request)?
        // 第二次 true  先弹一个框解释为什么需要权限,然后再去request,这个时候会出现不再询问
        // 第三次 当点击了不再询问会false不弹窗
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
            Log.i(TAG, "requestPermission shouldShowRequestPermissionRationale");
            shouldShowRationale(activity, requestCode, requestPermission);

        } else { //不需要弹窗(第一次) 要求权限打开弹窗
            Log.d(TAG, "requestCameraPermission else");
            ActivityCompat.requestPermissions(activity, new String[]{requestPermission}, requestCode);
        }
        //如该开启类权限,直接回调接口返回
    } else {
        Log.d(TAG, "ActivityCompat.checkSelfPermission ==== PackageManager.PERMISSION_GRANTED");
        permissionGrant.onPermissionGranted(requestCode);
    }
}

/**
 * 一次申请多个权限
 *
 * @param activity 上下文
 * @param grant 回调接口
 */
public static void requestMultiPermissions(final Activity activity, int[] permissions,PermissionGrant grant) {
    if(activity==null){
        return;
    }
    //获取没有授权 不弹窗的所有权限
    final List<String> permissionsList = getNoGrantedPermission(activity,permissions, false);
    //获取没有授权 弹窗的所有权限
    final List<String> shouldRationalePermissionsList = getNoGrantedPermission(activity,permissions, true);
    //如果没有需要打开的权限直接返回,
    if (permissionsList == null || shouldRationalePermissionsList == null) {
        return;
    }
    Log.d(TAG, "requestMultiPermissions permissionsList:" + permissionsList.size() + ",shouldRationalePermissionsList:" + shouldRationalePermissionsList.size());

    if (permissionsList.size() > 0) {
        //请求所有权限
        ActivityCompat.requestPermissions(activity, permissionsList.toArray(
                new String[permissionsList.size()]), CODE_MULTI_PERMISSION);
        Log.d(TAG, "showMessageOKCancel requestPermissions");

    } else if (shouldRationalePermissionsList.size() > 0) {
        showMessageOKCancel(activity, activity.getResources().getString(R.string.Dialog_Multi_Permission),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(activity, shouldRationalePermissionsList.toArray(
                                new String[shouldRationalePermissionsList.size()]), CODE_MULTI_PERMISSION);
                        Log.d(TAG, "showMessageOKCancel requestPermissions");
                    }
                });
    } else {
        grant.onPermissionGranted(CODE_MULTI_PERMISSION);
    }

}

/**
 * 获取没有授权  需要/不需要  弹窗的权限
 *
 * @param activity 上下文
 * @param isShouldRationale true 没有授权需要弹窗的权限      false 没有授权不需要弹窗的权限
 * @return 权限集合
 */
private static ArrayList<String> getNoGrantedPermission(Activity activity,int[] permissionCode, boolean isShouldRationale) {

    ArrayList<String> permissions = new ArrayList<>();

    for (int i = 0; i < permissionCode.length; i++) {
        String requestPermission = requestPermissions[permissionCode[i]];

        int checkSelfPermission = -1;
        try {
            checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
        } catch (RuntimeException e) {
            Toast.makeText(activity, activity.getResources().getText(R.string.Toast_permission), Toast.LENGTH_SHORT).show();
            Log.e(TAG, "RuntimeException:" + e.getMessage());
            return null;
        }

        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "getNoGrantedPermission ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED:" + requestPermission);

            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
                Log.d(TAG, "shouldShowRequestPermissionRationale if");
                if (isShouldRationale) {
                    permissions.add(requestPermission);
                }
            } else {
                if (!isShouldRationale) {
                    permissions.add(requestPermission);
                }
                Log.d(TAG, "shouldShowRequestPermissionRationale else");
            }
        }
    }
    return permissions;
}


/**
 * 权限返回结果,当请求权限结束的时候调用的方法(失败或者成功都会回调) --->打开设置权限
 *
 * @param activity   上下文
 * @param requestCode  包含的权限码
 * @param permissions  所有权限数组
 * @param grantResults 授权的权限数组
 * @param permissionGrant 回调接口
 */
public static void requestPermissionsResult(final Activity activity, final int requestCode, @NonNull String[] permissions,
                                            @NonNull int[] grantResults, PermissionGrant permissionGrant) {

    if (activity == null) {
        return;
    }
    //如果code是CODE_MULTI_PERMISSION就去调用多个权限的返回结果方法
    if (requestCode == CODE_MULTI_PERMISSION) {
        requestMultiResult(activity, permissions, grantResults, permissionGrant);
        return;
    }

    if (requestCode < 0 || requestCode >= requestPermissions.length) {
        Log.w(TAG, "requestPermissionsResult illegal requestCode:" + requestCode);
        Toast.makeText(activity, activity.getResources().getText(R.string.permission_illegal_code)+ ""+requestCode, Toast.LENGTH_SHORT).show();
        return;
    }

    Log.i(TAG, "onRequestPermissionsResult requestCode:" + requestCode + ",permissions:" + permissions.toString()
            + ",grantResults:" + grantResults.toString() + ",length:" + grantResults.length);


    if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "onRequestPermissionsResult PERMISSION_GRANTED");
        permissionGrant.onPermissionGranted(requestCode);
    } else {
        Log.i(TAG, "onRequestPermissionsResult PERMISSION NOT GRANTED");
        String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
        openSettingActivity(activity, "应用" + permissionsHint[requestCode]);
    }

}



/**
 * @param activity        上下文
 * @param permissions     权限
 * @param grantResults    授权的权限数组
 * @param permissionGrant 回调接口
 */
private static void requestMultiResult(Activity activity, String[] permissions, int[] grantResults, PermissionGrant permissionGrant) {

    if (activity == null) {
        return;
    }
    Log.d(TAG, "onRequestPermissionsResult permissions length:" + permissions.length);

    Map<String, Integer> perms = new HashMap<>();
    ArrayList<String> notGranted = new ArrayList<>();
    for (int i = 0; i < permissions.length; i++) {
        Log.d(TAG, "permissions: [i]:" + i + ", permissions[i]" + permissions[i] + ",grantResults[i]:" + grantResults[i]);
        perms.put(permissions[i], grantResults[i]);
        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
            notGranted.add(permissions[i]);
        }
    }

    if (notGranted.size() == 0) {
        permissionGrant.onPermissionGranted(CODE_MULTI_PERMISSION);
    } else {
        openSettingActivity(activity, activity.getResources().getString(R.string.Dialog_Multi_Permission));
    }

}

//显示请求权限弹窗
private static void shouldShowRationale(final Activity activity, final int requestCode, final String requestPermission) {
    String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
    showMessageOKCancel(activity, "应用" + permissionsHint[requestCode], new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ActivityCompat.requestPermissions(activity,
                    new String[]{requestPermission},
                    requestCode);
            Log.d(TAG, "showMessageOKCancel requestPermissions:" + requestPermission);
        }
    });
}



private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(context)
            .setMessage(message)
            .setPositiveButton("确定", okListener)
            .setNegativeButton("取消", null)
            .create()
            .show();
}


//打开设置页面
private static void openSettingActivity(final Activity activity, String message) {

    showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Log.d(TAG, "getPackageName(): " + activity.getPackageName());
            Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
            intent.setData(uri);
            activity.startActivity(intent);
        }
    });
}

四.权限的调用

  1. 如果项目使用的多的话,建议使用这种方式,建立一个基类定义一个接口回调权限的使用 建立一个BaseActivity,因为需要处理OnRequestPermissionsResultCallback的回调
public class BaseActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
PermissionUtils.PermissionGrant mPermissionGrant;

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
}

//权限回调
private OnPermissionListener OnPermissionListener;
public void setOnPermissionListener(OnPermissionListener OnPermissionListener){
    this.OnPermissionListener  = OnPermissionListener;
}
public interface OnPermissionListener{
    void openIntent();
}

//权限请求
public void openPermission(int[] code_permission){

    mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            if(OnPermissionListener!=null){
                OnPermissionListener.openIntent();
            }
        }
    };
    if(code_permission.length<=0){
        return;
    } else if(code_permission.length==1){
        PermissionUtils.requestPermission(BaseActivity.this, code_permission[0], mPermissionGrant);
    }else {
        PermissionUtils.requestMultiPermissions(BaseActivity.this,code_permission,mPermissionGrant);
    }

}


//请求权限回调方法(必须实现OnRequestPermissionsResultCallback接口)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionUtils.requestPermissionsResult(BaseActivity.this, requestCode, permissions, grantResults, mPermissionGrant);
}
}

2.子类基础BaseActivity之后,执行处理的方法

public class MainActivity extends BaseActivity implements View.OnClickListener {
    ....
    
     private void deleteCalendarEvent() {
            setOnPermissionListener(new OnPermissionListener() {
                @Override
                public void openIntent() {//设置权限监听之后,执行自己的操作
                    CalendarKit.delete(MainActivity.this, "2016-12-22");
                }
            });
            openPermission(new int[]{PermissionUtils.CODE_CALENDAR, PermissionUtils.CODE_STORAGE});//请求日历和内存卡权限
        }
}

非常感谢原文作者,如果想看具体的操作,点击文章一开始的链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容