我们在使用Android6.0以上的系统做测试的时候会发现这样一个问题:明明在AndroidManifest.xml文件中配置了应用所需要的权限,eg:读取短信,读写SDK,但是APP run上去之后提示没有权限,这样你的一些功能就无法正常的使用。出现这个的原因是,Android6.0以上的系统,有部分权限是危险权限。下面是危险权限和一些常规的权限:
Normal Permissions如下
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
Dangerous Permissions:
group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
group:android.permission-group.PHONE
permission: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.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
group:android.permission-group.CAMERA
permission:android.permission.CAMERA
group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS
group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO
group:android.permission-group.SMS
permission: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
以上在dangerous列表里头的权限在使用时得动态获取。值得注意的是,这里有一个分组的概念,比如你的应用,用到了读短信的权限,也用到了大宋短信的权限,但是,如下代码块,这些权限都在group:android.permission-group.SMS组里头,所以动态获取的时候,只需要获取该组里的任意一个。
group:android.permission-group.SMS
permission: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
Android 代码中动态获取危险权限:
- 列出你所需要动态获取的权限列表:
这些权限必须现在AndroidManifest.xml文件中注册
public class PermissionUtils {
private static Context mcontext;
private static PermissionUtils permissionUtils;
public static final int PERMISSION_REQUEST_CODE = 1001;
private PermissionUtils(){}
public static PermissionUtils getInstance(Context context){
mcontext = context;
if(permissionUtils == null){
permissionUtils = new PermissionUtils();
}
return permissionUtils;
}
/**
* 需要申请的权限列表
*/
public static final String[] BASIC_PERMISSIONS = new String[]{
Manifest.permission.READ_PHONE_STATE,
// Manifest.permission.CHANGE_WIFI_STATE,
// Manifest.permission.WAKE_LOCK,
// Manifest.permission.RECEIVE_BOOT_COMPLETED, //接收开机完成广播的权限
// Manifest.permission.GET_TASKS,
// Manifest.permission.SYSTEM_ALERT_WINDOW, //系统弹出框权限
// Manifest.permission.WRITE_EXTERNAL_STORAGE, //写SD卡的权限
Manifest.permission.READ_EXTERNAL_STORAGE, //读SD卡的权限
// Manifest.permission.ACCESS_COARSE_LOCATION, //获取位置信息的权限
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.ACCESS_FINE_LOCATION //获取位置信息的权限
};
/**
* 检查是否拥有指定的所有权限
*/
public boolean checkPermissionAllGranted(String[] permissions) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(mcontext, permission) != PackageManager.PERMISSION_GRANTED) {
// 只要有一个权限没有被授予, 则直接返回 false
return false;
}
}
return true;
}
public void requesetPermission(String[] permissions){
ActivityCompat.requestPermissions((Activity) mcontext,permissions,PERMISSION_REQUEST_CODE);
}
}
- 在activity的oncreate()方法中去获取:
//Android 6.0以上的系统动态申请权限
if(Build.VERSION.SDK_INT > 22){
if (!PermissionUtils.getInstance(this).checkPermissionAllGranted(BASIC_PERMISSIONS)){
PermissionUtils.getInstance(this).requesetPermission(BASIC_PERMISSIONS);
}
}
- 回调中处理
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
boolean isAllGranted = true;
// 判断是否所有的权限都已经授予了
for (int grant : grantResults) {
if (grant != PackageManager.PERMISSION_GRANTED) {
isAllGranted = false;
break;
}
}
if (isAllGranted) {
// 如果所有的权限都授予了, 则执行备份代码
} else {
// 弹出对话框告诉用户需要权限的原因, 并引导用户去应用权限管理中手动打开权限按钮
}
}
}