我无法在使用api 23的android中授予写入设置权限
我试图授予WRITE_设置权限,以允许我的应用程序在视图中增加亮度。我尝试了下面的代码,正如android文档中提到的,但每次没有允许用户接受或拒绝权限的弹出窗口时,我尝试调试代码grantResults[0]总是等于-1我无法在使用api 23的android中授予写入设置权限,android,kotlin,permissions,Android,Kotlin,Permissions,我试图授予WRITE_设置权限,以允许我的应用程序在视图中增加亮度。我尝试了下面的代码,正如android文档中提到的,但每次没有允许用户接受或拒绝权限的弹出窗口时,我尝试调试代码grantResults[0]总是等于-1 class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceS
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.
}
}
}
}
class MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//此处,此活动是当前活动
if(ContextCompat.checkSelfPermission(
这
Manifest.permission.WRITE\u设置
)!=已授予PackageManager.PERMISSION\u权限
) {
//未授予许可
//我们应该解释一下吗?
if(ActivityCompat.shouldShowRequestPermissionRegulation(这是Manifest.permission.WRITE_设置)){
//向用户显示解释*异步*--不阻止
//此线程正在等待用户的响应!在用户
//请查看说明,然后重试请求权限。
}否则{
//不需要解释,我们可以申请许可。
ActivityCompat.requestPermissions(
这
arrayOf(清单、权限、写入设置),
10
)
//我的\u权限\u请求\u读取\u联系人是
//应用程序定义的int常量。回调方法获取
//请求的结果。
}
}否则{
Settings.System.putInt(
这个.contentResolver,
Settings.System.SCREEN_亮度,255
)
}
}
重写onRequestPermissionsResult(请求代码:Int,
权限:数组,grantResults:IntArray){
何时(请求代码){
10 -> {
//如果取消请求,则结果数组为空。
if((grantResults.isNotEmpty()&&grantResults[0]==PackageManager.PERMISSION_已授予)){
Settings.System.putInt(
这个.contentResolver,
设置。系统。屏幕亮度,255
)
}否则{
//权限被拒绝,嘘!禁用
//依赖于此权限的功能。
}
返回
}
//添加其他“何时”行以检查其他
//此应用可能请求的权限。
其他->{
//忽略所有其他请求。
}
}
}
}
写入设置
不是可以通过requestPermissions()
请求的设置。这仅适用于危险
权限,而写入设置
没有该保护级别
引述:
注意:如果应用程序的目标API级别为23或更高,则应用程序用户必须通过权限管理屏幕向应用程序明确授予此权限。应用程序通过发送带有操作设置的意图来请求用户批准。操作\u管理\u写入\u设置
。应用程序可以通过调用Settings.System.canWrite()
来检查是否具有此授权
WRITE\u设置
不是可以通过requestPermissions()
请求的设置。这仅适用于危险
权限,而写入设置
没有该保护级别
引述:
注意:如果应用程序的目标API级别为23或更高,则应用程序用户必须通过权限管理屏幕向应用程序明确授予此权限。应用程序通过发送带有操作设置的意图来请求用户批准。操作\u管理\u写入\u设置
。应用程序可以通过调用Settings.System.canWrite()
来检查是否具有此授权