【问题标题】:I cannot grant WRITE_SETTING permissions in android with api 23我无法使用 api 23 在 android 中授予 WRITE_SETTING 权限
【发布时间】:2019-06-22 17:42:25
【问题描述】:

我尝试授予 WRITE_SETTING 权限以允许我的应用程序增加视图中的亮度我尝试了下面的代码,正如 android 文档中提到的那样,但每次没有弹出窗口允许用户接受或拒绝我的权限试图调试代码grantResults [0] 总是等于-1

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_SETTINGS
            ) != PackageManager.PERMISSION_GRANTED
        ) {

            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_SETTINGS)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.WRITE_SETTINGS),
                    10
                )

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            Settings.System.putInt(
                this.contentResolver,
                Settings.System.SCREEN_BRIGHTNESS, 255
            )
        }


    }

    override fun onRequestPermissionsResult(requestCode: Int,
                                            permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            10 -> {
                // If request is cancelled, the result arrays are empty.
                if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {

                    Settings.System.putInt(
                        this.contentResolver,
                        Settings.System.SCREEN_BRIGHTNESS,255
                    )
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return
            }

            // Add other 'when' lines to check for other
            // permissions this app might request.
            else -> {
                // Ignore all other requests.
            }
        }
    }

}

【问题讨论】:

    标签: android kotlin permissions


    【解决方案1】:

    WRITE_SETTINGS 不是您可以通过requestPermissions() 请求的。这仅适用于dangerous 权限,而WRITE_SETTINGS 没有protectionLevel

    引用the documentation:

    注意:如果应用面向 API 级别 23 或更高级别,则应用用户必须通过权限管理屏幕明确授予应用此权限。该应用程序通过发送带有操作Settings.ACTION_MANAGE_WRITE_SETTINGS 的意图来请求用户的批准。应用可以通过调用Settings.System.canWrite()来检查是否有这个权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多