动态权限<一>基本介绍

android 6.0以上为了保护用户的隐私,和以往被人诟病的权限机制,确立了新的权限机制。从 Android 6.0(API 级别 23)开始,用户开始在应用运行时向其授予权限,而不是在应用安装时授予。此方法可以简化应用安装过程,因为用户在安装或更新应用时不需要授予权限。它还让用户可以对应用的功能进行更多控制;例如,用户可以选择为相机应用提供相机访问权限,而不提供设备位置的访问权限。用户可以随时进入应用的“Settings”屏幕调用权限。

谷歌将权限分两类:1.正常权限。2.危险权限。

正常权限:

  • 正常权限不会直接给用户隐私权带来风险。如果应用在其清单中列出了正常权限,系统将自动授予该权限。

危险权限:

  • 危险权限会授予应用访问用户机密数据的权限。如果您的应用在其清单中列出了正常权限,系统将自动授予该权限。如果列出了危险权限,则用户必须明确允许应用使用这些权限。

那么对于一个应用要关注的就是那些危险权限,也就是我们所说的敏感权限。下表列出来有关的危险权限。

权限名称
1.calendar
2.camera
3.contacts
4.location
5.microphone 
6.phone
7.senors
8.sms
9.storage

 

兼容性来说,有以下细节:

1.低于API23 也就是系统6.0以下的,在manifest中注册申明权限清单,则自动授予权限,但是用户可以去设置中心关闭权限,但是不会引起应用运行异常

2.API23系统6.0以上的,需要在使用的时候去申请,没有申请就去使用则会引起应用运行异常。权限失效会导致 SecurityException 。

3.一开始是6.0以下系统应用,做了动态权限以后,会自动授予原先已有的权限,如果有新增的危险权限,需要做申请。

4.一开始就做了动态权限,再换回到6.0以下编译环境,新的apk无法安装。

下面以一个流程图说明问题。

危险权限的表单如下:

申请权限的工具类如下:

package com.nfdaily.nfplus.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

/**
 * Created by xilinch on 2017/5/3.
 * 对关键的权限进行申请  calendar /camera/ contacts/location/microphone /phone/senors/sms/storage
 * 增加对版本号的判断,大于等于23  (6.0)以上才进行权限的申请
 */

public class UtilRequestPermissions {


    /**
     * 拨打电话的请求码
     */
    public static final int REQUEST_CODE_CALL_PHONE = 0x9001;
    /**
     * 存储
     */
    public static final int REQUEST_CODE_READ_EXTERNAL_STORAGE = 0x9002;
    /**
     * 发送短信
     */
    public static final int REQUEST_CODE_SEND_SMS = 0x9003;
    /**
     * 传感器
     */
    public static final int REQUEST_CODE_BODY_SENSORS = 0x9004;
    /**
     * 录音
     */
    public static final int REQUEST_CODE_RECORD_AUDIO = 0x9005;
    /**
     * 定位
     */
    public static final int REQUEST_CODE_ACCESS_COARSE_LOCATION = 0x9006;
    /**
     * 相机
     */
    public static final int REQUEST_CODE_CAMERA = 0x9007;
    /**
     * 读取日历
     */
    public static final int REQUEST_CODE_READ_CALENDAR = 0x9008;
    /**
     * 录音
     */
    public static final int REQUEST_CODE_READ_CONTACTS = 0x9009;
    /**
     * 请求权限
     * @param activity
     * @param permission
     * @param requestCode
     */
    public static void requestPermission(Activity activity, String[] permission, int requestCode){
        ActivityCompat.requestPermissions(activity, permission, requestCode);

    }

    /**
     * 检查权限
     * @param activity
     * @param permission
     * @return
     */
    public static boolean checkSelfPermission(Activity activity, String permission){
        boolean isGranted = ActivityCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
        return isGranted;
    }

    /**
     * 日历
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionCalendar(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){

            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.READ_CALENDAR}, REQUEST_CODE_READ_CALENDAR);
            } else {
                requestPermission(activity, new String[]{Manifest.permission.READ_CALENDAR}, requestCode);
            }
        }

    }

    /**
     * 照相
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionCamera(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){

            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
            } else {
                requestPermission(activity, new String[]{Manifest.permission.CAMERA}, requestCode);
            }
        }
    }

    /**
     * 联系人
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionContacts(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){
            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE_READ_CONTACTS);
            } else {

                requestPermission(activity, new String[]{Manifest.permission.READ_CONTACTS}, requestCode);
            }
        }
    }

    /**
     * 定位
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionLocation(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){

            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE_ACCESS_COARSE_LOCATION);
            } else {

                requestPermission(activity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, requestCode);
            }
        }
    }

    /**
     * 录音
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionMicrophone(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){

            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_CODE_RECORD_AUDIO);
            } else {
                requestPermission(activity, new String[]{Manifest.permission.RECORD_AUDIO}, requestCode);
            }
        }
    }

    /**
     * 传感器
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionSensors(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){

            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.BODY_SENSORS}, REQUEST_CODE_BODY_SENSORS);
            } else {

                requestPermission(activity, new String[]{Manifest.permission.BODY_SENSORS}, requestCode);
            }
        }
    }

    /**
     * 短信
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionSms(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){

            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.SEND_SMS}, REQUEST_CODE_SEND_SMS);
            } else {
                requestPermission(activity, new String[]{Manifest.permission.SEND_SMS}, requestCode);

            }
        }
    }

    /**
     * 存储
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionStorage(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){

            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_READ_EXTERNAL_STORAGE);
            } else {
                requestPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);
            }
        }
    }

    /**
     * 电话 有关,包括READ_PHONE_STATE  /CALL_PHONE /READ_CALL_LOG/WRTITE_CALL_LOG/ADD_VOICEMAIL/USE_SIP/PROCESS_OUTGOING_CALLS
     * @param activity
     * @param requestCode
     */
    public static void requestPermissionPhone(Activity activity,int requestCode){
        if(Build.VERSION.SDK_INT >= 23){
            if(requestCode == 0){
                requestPermission(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CODE_CALL_PHONE);
            } else {
                requestPermission(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, requestCode);
            }
        }

    }

}

  

在下一部分将描述项目中实际处理的情况以及对比京东和淘宝的处理方案。

 

posted @ 2017-07-23 18:43  那些人追过的年  阅读(920)  评论(0编辑  收藏  举报