标签:
啥是运行时权限呢?Android M对权限管理系统进行了改版,之前我们的App需要权限,只需在manifest中申明即可,用户安装后,一切申明的权限都可来去自如的使用。但是Android M把权限管理做了加强处理,在manifest申明了,在使用到相关功能时,还需重新授权方可使用。当然,不是所有权限都需重新授权,所以就把这些需要重新授权方可使用的权限称之为运行时权限。
运行时权限的好处可以让用户使用时更有主动权,不会让app随便乱来。
但是受害最深的却是我们这些受苦受难的开发者,为何这么说呢?如果你的app的targetSdkVersion 是23也就是android 6.0的话,遇到运行时权限不去做代码处理的话,程序直接崩掉。所以,如果你的app没有在android 6.0上做足够的测试,请不要设置targetSdk为23,22以下就不会出现问题。
但是,作为开发者,我们还是必须要与时俱进的不是,而且以后的主流机型也必然是android M,怎么能不去适配他/她呢?继续往下看。
要先做好适配,那就必然要先了解哪些是运行时权限。
先看下哪些是不用特殊处理的权限,android称之为普通权限:
参考链接:http://developer.android.com/guide/topics/security/normal-permissions.html
As of API level 23, the following permissions are classified as PROTECTION_NORMAL:
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
再看下运行时权限,android称之为危险权限(google还对其分了组):
参考链接:http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
同一组的任何一个权限被授权了,其他权限也自动被授权。例如,一旦WRITE_CONTACTS被授权了,app也有READ_CONTACTS和GET_ACCOUNTS了。
------------------------------ 关键部分来了---------------------------------------------
目前对运行时权限,github上也有些代码封装库,但是都感觉用起来太麻烦,于是自己动手封装了下:
参考代码(google写的sample):
https://github.com/googlesamples/android-RuntimePermissions;
http://developer.android.com/training/permissions/requesting.html
在BaseActivity添加如下代码:
//**************** Android M Permission (Android 6.0权限控制代码封装)
*****************************************************
private int permissionRequestCode = 88;
private PermissionCallback permissionRunnable ;
public interface PermissionCallback{
void hasPermission();
void noPermission();
}
/**
* Android M运行时权限请求封装
* @param permissionDes 权限描述
* @param runnable 请求权限回调
* @param permissions 请求的权限(数组类型),直接从Manifest中读取相应的值,比如Manifest.permission.WRITE_CONTACTS
*/
public void performCodeWithPermission(@NonNull String permissionDes,PermissionCallback runnable,@NonNull String... permissions){
if(permissions == null || permissions.length == 0)return;
// this.permissionrequestCode = requestCode;
this.permissionRunnable = runnable;
if((Build.VERSION.SDK_INT < Build.VERSION_CODES.M) || checkPermissionGranted(permissions)){
if(permissionRunnable!=null){
permissionRunnable.hasPermission();
permissionRunnable = null;
}
}else{
//permission has not been granted.
requestPermission(permissionDes,permissionRequestCode,permissions);
}
}
private boolean checkPermissionGranted(String[] permissions){
boolean flag = true;
for(String p:permissions){
if(ActivityCompat.checkSelfPermission(this, p) != PackageManager.PERMISSION_GRANTED){
flag = false;
break;
}
}
return flag;
}
private void requestPermission(String permissionDes,final int requestCode,final String[] permissions){
if(shouldShowRequestPermissionRationale(permissions)){
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example, if the request has been denied previously.
// Snackbar.make(getWindow().getDecorView(), requestName,
// Snackbar.LENGTH_INDEFINITE)
// .setAction(R.string.common_ok, new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// ActivityCompat.requestPermissions(BaseAppCompatActivity.this,
// permissions,
// requestCode);
// }
// })
// .show();
//如果用户之前拒绝过此权限,再提示一次准备授权相关权限
new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage(permissionDes)
.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(BaseAppCompatActivity.this, permissions, requestCode);
}
}).show();
}else{
// Contact permissions have not been granted yet. Request them directly.
ActivityCompat.requestPermissions(BaseAppCompatActivity.this, permissions, requestCode);
}
}
private boolean shouldShowRequestPermissionRationale(String[] permissions){
boolean flag = false;
for(String p:permissions){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,p)){
flag = true;
break;
}
}
return flag;
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if(requestCode == permissionRequestCode){
if(verifyPermissions(grantResults)){
if(permissionRunnable!=null) {
permissionRunnable.hasPermission();
permissionRunnable = null;
}
}else{
showToast("暂无权限执行相关操作!");
if(permissionRunnable!=null) {
permissionRunnable.noPermission();
permissionRunnable = null;
}
}
}else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public boolean verifyPermissions(int[] grantResults) {
// At least one result must be checked.
if(grantResults.length < 1){
return false;
}
// Verify that each required permission has been granted, otherwise return false.
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
//********************** END Android M Permission ****************************************
Tips:
1)BaseActivity要继承AppCompatActivity
2)support包使用尽量新的,我使用的是compile ‘com.android.support:appcompat-v7:23.0.1‘ 以防里面的ActivityCompat找不到相关类或方法。
3)如果在Fragment中使用,直接在自己的BaseFragment写个方法调用此Activity的方法即可。
/**
* Android M运行时权限请求封装
* @param permissionDes 权限描述
* @param runnable 请求权限回调
* @param permissions 请求的权限(数组类型),直接从Manifest中读取相应的值,比如Manifest.permission.WRITE_CONTACTS
*/
public void performCodeWithPermission(@NonNull String permissionDes,BaseAppCompatActivity.PermissionCallback runnable,@NonNull String... permissions){
if(getActivity()!=null && getActivity() instanceof BaseAppCompatActivity){
((BaseAppCompatActivity) getActivity()).performCodeWithPermission(permissionDes,runnable,permissions);
}
}
比如,我们要请求相机:
performCodeWithPermission("XX App请求访问相机权限",new BaseAppCompatActivity.PermissionCallback() {
@Override
public void hasPermission() {
//执行打开相机相关代码
}
@Override
public void noPermission() {
}
}, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE);
===20160509更新========
要改成如下的检测方案:
参考:http://stackoverflow.com/questions/33407250/checkselfpermission-method-is-not-working-in-targetsdkversion-22
public boolean selfPermissionGranted(String permission) {
// For Android < Android M, self permissions are always granted.
boolean result = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (targetSdkVersion >= Build.VERSION_CODES.M) {
// targetSdkVersion >= Android M, we can
// use Context#checkSelfPermission
result = context.checkSelfPermission(permission)
== PackageManager.PERMISSION_GRANTED;
} else {
// targetSdkVersion < Android M, we have to use PermissionChecker
result = PermissionChecker.checkSelfPermission(context, permission)
== PermissionChecker.PERMISSION_GRANTED;
}
}
return result;
}
获取target sdk的方法如下:
try {
final PackageInfo info = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
targetSdkVersion = info.applicationInfo.targetSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
google自己出了一个解决方案,在github上叫easypermissions。
链接为:https://github.com/googlesamples/easypermissions
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/5667068.html