Android SDK6.0 运行时权限问题

【Android SDK6.0 运行时权限问题】莫道桑榆晚,为霞尚满天。这篇文章主要讲述Android SDK6.0 运行时权限问题相关的知识,希望能为你提供帮助。
1.最近在SDK6.0开发项目组遇到一个这样的问题:requires  Android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
2.原因:从6.0(api 23)开始, android采用了Runtime permission request, 即在运行时请求权限,而不是在编译的时候,即使在manifest.xml里面添加了权限也要在运行时请求。
    但不是所有的permission都要Runtime request,涉及到资源文件请求时就会用到Runtime request,比如获取位置信息, content provider获取media的情况。
3.解决方法:
a) 首先确认在manefist里面添加了READ_EXTERNAL_STORAGE权限。
  b) 在代码中实现Runtime request:

1 @SuppressLint("NewApi") 2 private void requestReadExternalPermission() { 3if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) 4!= PackageManager.PERMISSION_GRANTED) { 5Log.d(TAG, "READ permission IS NOT granted..."); 6 7if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) { 8 9Log.d(TAG, "11111111111111"); 10} else { 11// 0 是自己定义的请求coude 12requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0); 13Log.d(TAG, "222222222222"); 14} 15} else { 16Log.d(TAG, "READ permission is granted..."); 17} 18 }

1c)处理请求: 2 3 @Override 4 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 5Log.d(TAG, "requestCode=" + requestCode + "; ---> " + permissions.toString() 6+ "; grantResult=" + grantResults.toString()); 7switch (requestCode) { 8case 0: { 9 10if (grantResults.length > 0 11& & grantResults[0] == PackageManager.PERMISSION_GRANTED) { 12 13// permission was granted 14// request successfully, handle you transactions 15 16} else { 17 18// permission denied 19// request failed 20} 21 22return; 23} 24default: 25break; 26 27} 28 } 29

 


    推荐阅读