Hiten's Blog.

Android6.0运行时权限总结

字数统计: 769阅读时长: 3 min
2016/05/14 Share

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app’s functionality

上面是引用google官方文档的一段话,google将权限分为两类,一类是一般权限,一类是危险权限
一般权限有哪些,参加官方文档https://developer.android.com/guide/topics/security/normal-permissions.html
这些是一般的权限,看看那些是你认识的

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
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_IGNORE_BATTERY_OPTIMIZATIONS
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

对于危险权限,参考文档:https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

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
Permission Group	Permissions
CALENDAR
READ_CALENDAR
WRITE_CALENDAR
CAMERA
CAMERA
CONTACTS
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
LOCATION
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
MICROPHONE
RECORD_AUDIO
PHONE
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
SENSORS
BODY_SENSORS
SMS
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE

从官网粘贴过来的表格数据格式一塌糊涂,我了个擦擦

险权限为毛要分组,googl的一句话:All dangerous Android system permissions belong to permission groups.好吧
分就分吧,注意,授权机制只对6.0以上的机器有效,授权机制如下:

如果一个APP申请一个在manifest文件中申明过的危险权限,如果这个权限没被授予,会谈Dialgo提示用户,用户点击确认方可授权
如果一个APP申请一个在manifest文件中申明过的危险权限,如果这个权限被授予过,就直接授予,不用再弹框提醒;
如果APP已经授予过READ_CONTACTS权限,那么申请WRITE_CONTACTS权限时,也会直接授予,就是在一个组里的,只要授予一个了,其他的都授。

相关API参考

  • 检查权限

    To check if you have a permission, call the ContextCompat.checkSelfPermission() method. For example, this snippet shows how to check if the activity has permission to write to the calendar:

1
2
3
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);

方法主要返回值为PackageManager.PERMISSION_GRANTED或者PERMISSION_DENIED,如果返回PERMISSION_GRANTED表示已授权,如果返回PERMISSION_DENIED,表示没有被授权

解释为什么app需要授权

  • 申请授权
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
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {

// 我们需要向用户解释为什么需要这个授权,主要是用户已经拒绝过一次了
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {

// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.

} else {

//不需要解释,
//请求权限,请求了权限一个数组
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS 是程序员自己定义的一个常量,在结果回调时判断,类似于startActivityForResult()方法中的requestCode

}
}

,其实主要有两个方法ActivityCompat.shouldShowRequestPermissionRationale和ActivityCompat.requestPermissions

  • 处理回调
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
//grantResults对应于请求权限的那个String[]
switch (requestCode) {
//对应刚才那个请求code
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// 如果权限取消,数组是空的
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//获取权限成功
} else {
//获取权限失败
}
return;
}

//其他请求
}
}

总结:6.0运行权限其实是很简单的API,在项目中也用了起来,下一步可以进行封装,再接再厉!

CATALOG