Android6.0 动态权限(Runtime Permission)

Android6.0 动态权限

Android 6.0中将Runtime Permission分成了普通权限(Normal Permission)以及危险权限(Dangerous Permission),普通权限在AndroidManifest.xml中注册的时候是默认打开的,而被认为危险的权限在注册的时候是关闭的,需要用户去打开或者在代码里动态申请。当APP的API < 23时,危险权限还是默认打开的,只有当APP的API >= 23的时候,新的Runtime Permission的套路才会有效果。

危险权限(Dangerous Permission)

危险权限一共有以下几种:

身体传感器,日历,摄像头,通讯录,地理位置,麦克风,电话,短信,存储空间.

在Framework中默认打开Dangerous Permission

有些系统程序他申请了危险权限,但是很多情况下他是默认打开的,比如原生Camera的Camera,Storage等权限是默认打开的,不需要用户去动态打开它或者在代码里申请打开它。那么他是怎么做到的?其实在framework中有一个配置文件可以默认去为系统APP打开危险权限。 
这个文件的全路径: 
/myandroid/frameworks/base/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java

// Camera  Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  PackageParser.Package cameraPackage = getDefaultSystemHandlerActivityPackageLPr(          cameraIntent, userId);  if (cameraPackage != null          && doesPackageSupportRuntimePermissions(cameraPackage)) {      grantRuntimePermissionsLPw(cameraPackage, CAMERA_PERMISSIONS, userId);      grantRuntimePermissionsLPw(cameraPackage, MICROPHONE_PERMISSIONS, userId);      grantRuntimePermissionsLPw(cameraPackage, STORAGE_PERMISSIONS, userId);  }

从上面这段代码就可以看到Camera中默认打开了CAMERA_PERMISSIONS, MICROPHONE_PERMISSIONS, STORAGE_PERMISSIONS这三个Dangerous Permission。

//在系统APP Browser中加入默认Sotrage权限。grantRuntimePermissionsLPw(browserPackage, STORAGE_PERMISSIONS, userId);

在APP中动态申请Dangerous Permission

这些关于权限的API都是在Activity中调用的。我这里使用的API是23,在support包里也有一套一样的API。

检查权限是否Grant

if(checkSelfPermission(Manifest.permission.RECORD_AUDIO)               != PackageManager.PERMISSION_GRANTED){       mNumPermissionsToRequest++;       mShouldRequestSoundPermission = true;   }

申请权限

if(mShouldRequestStoragePermission){       permissionToRequest[permissionRequestIndex] =   Manifest.permission.WRITE_EXTERNAL_STORAGE;       mIndexPermissionRequestStorage= permissionRequestIndex;       permissionRequestIndex++;   }    //关键是这句,调用这个API之后会出现如下的对话框,这个对话框是系统UI,不需要APP来写   if(permissionToRequest.length > 0){       requestPermissions(permissionToRequest, PERMISSION_REQUEST_CODE);   }

这里写图片描述

授权(Allow)或拒绝授权(Deny)后会在Activity回调这个方法

@Overridepublic void onRequestPermissionsResult(int requestCode,      String permissions[], int[] grantResults) {       switch (requestCode) {       case PERMISSION_REQUEST_CODE:           if (grantResults.length > 0                   && grantResults[0] == PackageManager.PERMISSION_GRANTED) {               Log.v(TAG, "Grant permission successfully");           } else {               Log.v(TAG, "Grant permission unsuccessfully");           }           break;       default:           break;   }}

为了使用方便,我自己封装了一个小工具,贴出代码

import java.util.List;import java.util.LinkedList;import android.app.Activity;import android.Manifest;import android.util.Log;import android.content.pm.PackageManager;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import com.freescale.cactusplayer.R;public class PermissionChecker {    private Activity mActivity;    private String[] mPermissions;    private PermissionCheckCallback mCallback;    private static final String TAG = "PermissionChecker";    private static final int REQUST_CODE = 0;    private boolean isDefaultDialog = false;    public PermissionChecker setActivity(Activity activity) {        this.mActivity = activity;        return this;    }    public PermissionChecker setPermissions(String[] permissions) {        this.mPermissions = permissions;        return this;    }    public PermissionChecker setCallback(PermissionCheckCallback callback) {        this.mCallback = callback;        return this;    }    public PermissionChecker setDefaultDialog(boolean isDefaultDialog) {        this.isDefaultDialog = isDefaultDialog;        return this;    }    public boolean isPermissionGrant(String permission) {        return mActivity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;    }    public void checkPermission() {        if (mPermissions == null || mActivity == null || mPermissions == null)            return;        List permissionToRequestList = new LinkedList();        for (String permission : mPermissions) {            if(mActivity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)                permissionToRequestList.add(permission);        }        String[] permissionToRequest = (String[])permissionToRequestList.toArray(new String[permissionToRequestList.size()]);        if(permissionToRequest.length > 0){            mActivity.requestPermissions(permissionToRequest, REQUST_CODE);            if (mCallback != null)                mCallback.onRequest();        } else {            if (mCallback != null) {                mCallback.onGranted();            }        }    }    public interface PermissionCheckCallback {        void onRequest();        void onGranted();        void onGrantSuccess();        void onGrantFail();    }    public void onRequestPermissionsResult(int requestCode,            String permissions[], int[] grantResults) {        switch (requestCode) {        case REQUST_CODE:            if (grantResults.length > 0                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                Log.v(TAG, "Grant permission successfully");                if (mCallback != null)                    mCallback.onGrantSuccess();            } else {                if (isDefaultDialog) {                    popupWarningDialog();                    return;                }                if (mCallback != null) {                    mCallback.onGrantFail();                }            }            break;        default:            break;        }    }    private void popupWarningDialog(){        DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){            @Override            public void onClick(DialogInterface dialog, int which) {                switch(which){                case Dialog.BUTTON_POSITIVE:                    if (mCallback != null)                         mCallback.onGranted();                    break;                case Dialog.BUTTON_NEGATIVE:                    mActivity.finish();                    break;                default:                    break;                }            }        };        AlertDialog.Builder builder=new AlertDialog.Builder(mActivity);        builder.setTitle(R.string.Warning);        builder.setMessage(R.string.PermissionNotGrant);        builder.setPositiveButton(R.string.OK,dialogOnclicListener);        builder.setNegativeButton(R.string.Cancel, dialogOnclicListener);        builder.create().show();    }   }

使用的方法

//new一个PermissionChecker然后填入相应的权限和CallbackmPermissionChecker = new PermissionChecker();mPermissionChecker.setActivity(this)                       .setDefaultDialog(true)                       .setPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE})                       .setCallback(new PermissionCheckCallback() {                           @Override                           public void onRequest() {                           }                           @Override                           public void onGranted() {                               initView();                           }                           @Override                           public void onGrantSuccess() {                               initView();                           }                           @Override                           public void onGrantFail() {                           }                       }).checkPermission();//回调的时候调用PermissionChecker的onRequestPermissionsResult方法@Overridepublic void onRequestPermissionsResult(int requestCode,        String permissions[], int[] grantResults) {       mPermissionChecker.onRequestPermissionsResult(requestCode, permissions, grantResults);}

你可能感兴趣的:(Android)