安卓7.1的亮度调节问题

最近在适配公司7.1的时候,发现6.0之前设置亮度的方法在7.1上面报错:

java.lang.SecurityException: com.autonavi.minimap was not granted this permission: android.permission.WRITE_SETTINGS.
用requestPermission也无法获取到权限。后来找到了解决办法,打开系统设置页面,才有这个权限的设置。

 解决方法如下:

private fun CheckPermission() {
    if (!Settings.System.canWrite(activity)) {
        val selfPackageUri: Uri = Uri.parse(
            "package:"
                    + activity!!.packageName
        )
        val intent = Intent(
            Settings.ACTION_MANAGE_WRITE_SETTINGS,
            selfPackageUri
        )
        startActivity(intent)
    }
}

再贴上设置亮度的方式:

//设置亮度
fun setScreenBrightness(process: Int, context: Context,Exception: (() -> Unit)? = null) {
    saveBrightness(context.contentResolver, process,Exception)
}

private fun saveBrightness(resolver: ContentResolver, brightness: Int,Exception: (() -> Unit)? = null) {
    try {
        Settings.System.putInt(
            resolver, Settings.System.SCREEN_BRIGHTNESS,
            Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
        )
        val uri =
            Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS)

        Settings.System.putInt(
            resolver,
            Settings.System.SCREEN_BRIGHTNESS,
            brightness
        )
        resolver.notifyChange(uri, null)
    }catch (e:Exception){
        Exception!!.invoke()
    }

} 

最后记得添加权限:

你可能感兴趣的:(安卓7.1的亮度调节问题)