Skip to content

Instantly share code, notes, and snippets.

@Piasy
Created February 20, 2016 00:47
Show Gist options
  • Save Piasy/11948c71839dd87c8898 to your computer and use it in GitHub Desktop.
Save Piasy/11948c71839dd87c8898 to your computer and use it in GitHub Desktop.
Android Runtime Permission test
<uses-permission android:name="android.permission.READ_CONTACTS"/>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 继续
}
switch (checkSelfPermission(Manifest.permission.READ_CONTACTS)) {
case PackageManager.PERMISSION_GRANTED:
// 已有授权
break;
case PackageManager.PERMISSION_DENIED:
// 没有权限:尚未请求过权限,或者请求授权被拒绝,或者曾经授权过,
// 但被用户在设置中禁用权限
break;
default:
// 其实只会返回上述两种情况
break;
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSION) {
if (permissions.length == 1 &&
permissions[0].equals(Manifest.permission.READ_CONTACTS) &&
grantResults.length == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 授权请求被通过,读取通讯录
} else {
// 授权请求被拒绝
}
} else {
// 其他情况
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
requestPermissions(new String[] { Manifest.permission.READ_CONTACTS },
REQUEST_PERMISSION);
public boolean shouldShowRequestPermissionRationale(
@NonNull String permission)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment