标题:Android 6.0 运行权限解析
作者:Android轮子哥
原文链接:Android 6.0 运行权限解析 - 简书
...
系统权限根据敏感程度分为普通权限和危险权限两类。两类权限都需要在 AndroidManifest.xml 文件中申明。在 Android 5.1 (API level 22) 及其以下,系统在 App 安装时要求用户授权所有权限,否则 App 不能安装,而在 Android 6.0 及其以上版本上,系统在APP安装时授权所有普通权限,危险权限需要在使用时动态让用户授权。这使得 Android 的权限管理更加灵活,用户可以根据需要在设置应用中对应用的各个危险权限授予不同的权限。
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
// 日历
public static final String[] CALENDAR_GROUP = {Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR};
// 摄像头
public static final String[] CAMERA_GROUP = {Manifest.permission.CAMERA};
// 联系人
public static final String[] CONTACTS_GROUP = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.GET_ACCOUNTS};
// 位置
public static final String[] LOCATION_GROUP = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
// 话筒
public static final String[] MICROPHONE_GROUP = {Manifest.permission.RECORD_AUDIO};
// 电话
public static final String[] PHONE_GROUP = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE, Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG, Manifest.permission.ADD_VOICEMAIL, Manifest.permission.USE_SIP, Manifest.permission.PROCESS_OUTGOING_CALLS};
// 传感器
public static final String[] SENSORS_GROUP = {Manifest.permission.BODY_SENSORS};
// 短信
public static final String[] SMS_GROUP = {Manifest.permission.SEND_SMS, Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_WAP_PUSH, Manifest.permission.RECEIVE_MMS};
// 存储
public static final String[] STORAGE_GROUP = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
implementation 'com.android.support:appcompat-v7:25.3.1'
/**
* 授权了
*/
public static final int PERMISSION_GRANTED = 0;
/**
* 拒绝了
*/
public static final int PERMISSION_DENIED = -1;
/**
* 检测某个权限是否授予
* @param context Context对象
* @param permission 需要检测的权限
*/
ContextCompat.checkSelfPermission(Context context, String permission);
//又或者使用子类的方法
ActivityCompat.checkSelfPermission(Context context, String permission);
//minSdkVersion >= 23 可以直接使用
activity.checkSelfPermission(String permission);
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED){
}
/**
* 申请相关权限
* @param activity Activity对象
* @param permissions 请求的权限组
* @param requestCode 本次请求码
*/
ActivityCompat.requestPermissions(Activity activity, String[] permissions, int requestCode);
//minSdkVersion >= 23 可以直接使用
activity.requestPermissions(String[] permissions, int requestCode);
/**
* 是否需要向用户解释
* @param activity Activity对象
* @param permission 需要检测的权限
*/
ActivityCompat.shouldShowRequestPermissionRationale(Activity activity, String permission);
//minSdkVersion >= 23 可以直接使用
activity.shouldShowRequestPermissionRationale(String permission);
public static boolean shouldShowRequestPermissionRationale(@NonNull Activity activity, @NonNull String permission) {
if (Build.VERSION.SDK_INT >= 23) {
return ActivityCompatApi23.shouldShowRequestPermissionRationale(activity, permission);
}
return false;
}
/**
* Activity处理权限结果回调
* @param requestCode 权限请求码
* @param permissions 请求的权限组
* @param grantResults 请求的结果
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {}
for (int i = 0; i < grantResults.length ; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
startActivity(intent);