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() {
 3     if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
 4             != PackageManager.PERMISSION_GRANTED) {
 5         Log.d(TAG, "READ permission IS NOT granted...");
 6         
 7         if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
 8             
 9             Log.d(TAG, "11111111111111");
10         } else {
11             // 0 是自己定义的请求coude
12             requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
13             Log.d(TAG, "222222222222");
14         }
15     } else {
16         Log.d(TAG, "READ permission is granted...");
17     }
18 }
 1    c)处理请求:
 2     
 3 @Override
 4 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
 5     Log.d(TAG, "requestCode=" + requestCode + "; --->" + permissions.toString()
 6             + "; grantResult=" + grantResults.toString());
 7     switch (requestCode) {
 8         case 0: {
 9             
10             if (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             
22             return;
23         }
24         default:
25             break;
26 
27     }
28 }
29 

 

Android SDK6.0 运行时权限问题

上一篇:安卓手机调试日志


下一篇:Android将被依赖的库项目打包成aar(非jar)文件