Android 6.0前后权限问题

  • github 上下载了别人的源码,看到特意针对 android 6.0区别处理了,不清楚为什么这样做,难道 6.0 以下是不需要申请权限的?
  • 下面是我下载的源码

      package com.mobile.mobileinfo.util;
    
      import android.Manifest;
      import android.app.Activity;
      import android.content.Context;
      import android.content.pm.PackageManager;
      import android.os.Build;
    
      import java.util.ArrayList;
      import java.util.List;
    
      public class PermissionUtil {
    
          // 动态权限申请
          public static void checkPermission(Activity context) {
              if (Build.VERSION.SDK_INT >= 23) {
                  List<String> permissions = null;
                  if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                      permissions = new ArrayList<>();
                      permissions.add(Manifest.permission.READ_PHONE_STATE);
                  }
                  if (context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                      if (permissions == null) {
                          permissions = new ArrayList<>();
                      }
                      permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
                  }
                  if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                      if (permissions == null) {
                          permissions = new ArrayList<>();
                      }
                      permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                  }
    
                  if (context.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                      if (permissions == null) {
                          permissions = new ArrayList<>();
                      }
                      permissions.add(Manifest.permission.CAMERA);
                  }
                  if (permissions != null) {
                      String[] permissionArray = new String[permissions.size()];
                      permissions.toArray(permissionArray);
                      context.requestPermissions(permissionArray, 0);
                  }
              }
          }
      }
    
  • 有必要的话:QQ = 409223171
阅读 409
评论
    1 个回答
    • 11.2k

    以前是在xml里静态声明的,无法动态许可。

      撰写回答

      登录后参与交流、获取后续更新提醒