月度归档:2015年10月

Android M(6.0) 权限爬坑之旅

有一篇全面介绍Android M 运行时权限文章写的非常全面:Android M 新的运行时权限开发者需要知道的一切,但是实施过程中还是遇到一些坑。

坑一:用Android5.0编译的apk,在Android6.0上运行完全没有问题。

  1. 在Android6.0以上才需要在运行时请求权限,在旧Android版本上保留原有逻辑,安装时授予权限。
  2. 用旧版本sdk编译的apk,都使用旧版本权限方式,安装时授予权限。(也就是说:兼容旧版本)
  3. 用Android6.0(targetSdkVersion 23)作为目标版本才需要处理新的权限问题
  4. 还有一个蛋疼的问题:程序运行当中,用户关闭了权限,会发生什么?(还未知)

坑二:蓝牙扫描需要位置权限,而且定位要开启

BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results

如何申请一个权限:用ACCESS_COARSE_LOCATION举栗子

1. Manifest添加权限

Android6.0分了几种权限:Normal Permissions(安装时自动授权,用户也不能取消权限) and Dangerous Permissions

    <!-- Android6.0 蓝牙扫描才需要-->
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
2. 请求权限
//判断是否有权限
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED)

//请求权限
ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
//判断是否需要 向用户解释,为什么要申请该权限
ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.READ_CONTACTS)
//权限申请结果
onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults)

坑三:Fragment请求权限

//如果使用ActivityCompat.requestPermissions,不会调用onRequestPermissionsResult()
//请求权限
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
//判断是否需要 向用户解释,为什么要申请该权限
shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)

坑四:WRITE_SETTINGS权限怎么处理

android.permission.WRITE_SETTINGS不能自动授权,也不能运行时请求授权,咋整啊?通过打开Intent来让用户设置。貌似SETTINGS的权限只能这么处理,from CommonsWare research Android 6.0 changes.

    /**
     * An app can use this method to check if it is currently allowed to write or modify system
     * settings. In order to gain write access to the system settings, an app must declare the
     * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its manifest. If it is
     * currently disallowed, it can prompt the user to grant it this capability through a
     * management UI by sending an Intent with action
     * {@link android.provider.Settings#ACTION_MANAGE_WRITE_SETTINGS}.
     *
     * @param context A context
     * @return true if the calling app can write to system settings, false otherwise
     */
     if(!Settings.System.canWrite(this)){
          Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                    Uri.parse("package:" + getPackageName()));
          startActivityForResult(intent, REQUEST_CODE);
     } 


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE) {
            if (Settings.System.canWrite(this)) {
                //检查返回结果
                Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission not granted", Toast.LENGTH_SHORT).show();
            }
        }
    }

源码地址

GitHub地址:Android_M_requestPermissions