Created
February 20, 2016 00:47
-
-
Save Piasy/11948c71839dd87c8898 to your computer and use it in GitHub Desktop.
Android Runtime Permission test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.READ_CONTACTS"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
// 继续 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch (checkSelfPermission(Manifest.permission.READ_CONTACTS)) { | |
case PackageManager.PERMISSION_GRANTED: | |
// 已有授权 | |
break; | |
case PackageManager.PERMISSION_DENIED: | |
// 没有权限:尚未请求过权限,或者请求授权被拒绝,或者曾经授权过, | |
// 但被用户在设置中禁用权限 | |
break; | |
default: | |
// 其实只会返回上述两种情况 | |
break; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requestPermissions(new String[] { Manifest.permission.READ_CONTACTS }, | |
REQUEST_PERMISSION); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean shouldShowRequestPermissionRationale( | |
@NonNull String permission) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment