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
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。
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++;
}
if(permissionToRequest.length > 0){
requestPermissions(permissionToRequest, PERMISSION_REQUEST_CODE);
}
授权(Allow)或拒绝授权(Deny)后会在Activity回调这个方法
@Override
public 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<String> permissionToRequestList = new LinkedList<String>();
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
使用的方法
mPermissionChecker = 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();
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
mPermissionChecker.onRequestPermissionsResult(requestCode, permissions, grantResults);