设为首页 收藏本站 662p开源网
开启辅助访问 切换到宽版

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

切换风格 立即注册 找回密码

安卓教程网

查看: 12043|回复: 1

Android运行时权限

[复制链接]
发表于 2017-12-29 18:06:51 | 显示全部楼层 |阅读模式
更多
 在android6.0以后,Google将权限分为了两类,一类为正常权限(Normal Permission),一类为危险权限(Dangerous Permission)。对于正常正常权限,只需在Manifest文件里面申请即可,但对于危险权限,需要运行时动态申请。
危险权限包括以下:
权限组权限
group:android.permission-group.CONTACTSpermission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
group:android.permission-group.PHONEpermission: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.Calendarpermission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
group:android.permission-group.Camerapermission:android.permission.CAMERA
group:android.permission-group.SENSORSpermission:android.permission.BODY_SENSORS
group:android.permission-group.LOCATIONpermission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
group:android.permission-group.STORAGEpermission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
group:android.permission-group.MICROPHONEpermission:android.permission.RECORD_Audio
group:android.permission-group.SMSpermission: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
动态权限申请:
  1、在manifest文件里面配置所有需要申请的权限(包括普通权限和危险权限)
  2、确保工程TargetSdkVersion版本在23以上(因为动态权限是针对23以上的版本设计的,如果TargetSdkVersion低于23,相关接口将会失效,比如ActivityCompat.checkSelfPermission将始终返回0)
  3、确保目标设备为Android6.0及以上
  4、检查权限:ActivityCompat.checkSelfPermission检查权限是否被允许。如果权限被拒绝,在使用ActivityCompat.shouldShowRequestPermissionRationale检查权限是否被彻底拒绝。
  5、权限申请:如果权限没有被彻底拒绝,则可以使用ActivityCompat.requestPermissions申请权限
  6、后续处理:当用户处理完权限请求后,可以通过onRequestPermissionsResult获取到用户处理的结果

示例代码:
  检查TargetSdkVersion和目标设备API版本:
  1. private static int getTargetSdkVersion(Activity activity){
  2.         int targetSdkVersion = 0;
  3.         Context context = activity.getApplicationContext();
  4.         try {  
  5.             final PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);  
  6.             targetSdkVersion = info.applicationInfo.targetSdkVersion;  
  7.         } catch (PackageManager.NameNotFoundException e) {  
  8.             e.printStackTrace();  
  9.         }  
  10.         return targetSdkVersion;
  11.     }
  12.    
  13.     private static boolean checkRuntimePermissionAvailable(Activity activity){
  14.         // Always return true for API version < M, let the system deal with the permissions
  15.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  16.             Log.w("Permission", "API version < M, return true by default");
  17.             return false;
  18.         }
  19.         // Always return true for target sdk version < M, let the system deal with the permissions
  20.         if (getTargetSdkVersion(activity) < Build.VERSION_CODES.M) {
  21.             Log.w("Permission", "Target sdk version < M, return true by default");
  22.             return false;
  23.         }
  24.         return true;
  25.     }
复制代码

获得被拒绝的权限:
  1. @SuppressLint("NewApi")
  2.     private static String[] getDeniedPermissions(Activity activity, String[] permissions, boolean isAll) {
  3.         List<String> deniedPermissionList = new ArrayList<String>();
  4.         Context context = activity.getApplicationContext();
  5.         for (String permission : permissions){
  6.             if (ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED){
  7.                 Log.i("Permission", "\"" + permission + "\" has been granted");
  8.             } else {
  9.                 if (ActivityCompat.shouldShowRequestPermissionRationale(thisClass, permission)){
  10.                     deniedPermissionList.add(permission);
  11.                     Log.w("Permission", "\"" + permission + "\" was denied");
  12.                 }else{
  13.                     if (isAll){
  14.                         deniedPermissionList.add(permission);
  15.                     }
  16.                     Log.w("Permission", "\"" + permission + "\" was denied completely");
  17.                 }
  18.             }
  19.         }
  20.         
  21.         String[] deniedPermissions = new String[deniedPermissionList.size()];
  22.         deniedPermissions = deniedPermissionList.toArray(deniedPermissions);

  23.         return deniedPermissions;
  24.     }
复制代码
 申请被拒绝的权限:
  1. @SuppressLint("NewApi")
  2.     private static void requestPermissions(Activity activity, int requestCode, String[] deniedPermissions){
  3.         for( String permission : deniedPermissions ){
  4.             Log.i("Permission", "Requesting permission \"" + permission + "\"");
  5.         }
  6.         ActivityCompat.requestPermissions(activity, deniedPermissions, requestCode);
  7.     }
复制代码

权限申请的后续处理:
  1. @SuppressLint("NewApi")
  2.     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  3.         // TODO Auto-generated method stub
  4.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);

  5.         boolean isAllGranted = true;
  6.         Switch( requestCode ){
  7.         case 1:
  8.             for ( int result : grantResults){
  9.                 if (result != PackageManager.PERMISSION_GRANTED){
  10.                     isAllGranted = false;
  11.                     break;
  12.                 }
  13.             }
  14.             break;
  15.         }
  16.         
  17.         if (isAllGranted){
  18.             // All permissions are granted
  19.             Log.i("Permission", "All permissions have been granted");
  20.         }else{
  21.             // There is at least a permission which aren't granted
  22.             Log.w("Permission", "There is at least a permission which aren't granted");
  23.         }
  24.     }
复制代码




新浪微博达人勋

注册会员

93

积分

31

威望

0

金钱

Rank: 2

积分
93
发表于 2017-12-29 18:07:52 | 显示全部楼层
回个帖子,下班咯~
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Archiver|手机版|小黑屋|安卓教程网 ( 粤ICP备12089784号  

GMT+8, 2018-1-19 05:37 , Processed in 0.497927 second(s), 46 queries .

Powered by Discuz X3.1

© 2001-2013 IOS教程网

快速回复 返回顶部 返回列表