权限检查SET_ALARM总是返回PERMISSION_DENIED而不询问用户在Android Studio中的权限,Kotlin

我正在尝试使用Kotlin在Android Studio中为我的应用设置闹钟。 但是,运行alarmManager.setRepeating(...)不起作用, alarmManager.nextAlarmClock返回null。 我发现,当我试图检查SET_ALARM权限时,我得到了PERMISSION_DENIED (-1),即使我已经指定在清单中使用它。 ActivityCompat.requestPermission不会打开警报,因此用户无法授予权限。 我怎样才能让我的应用程序允许SET_ALARM ? 代码如下:

 val permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.SET_ALARM) if(permissionCheck != PackageManager.PERMISSION_GRANTED) { Log.d("Notification", "Permission not granted earlier, permission: $permissionCheck") ActivityCompat.requestPermissions(this, arrayOf<String>(android.Manifest.permission.SET_ALARM),1) } else { Log.d("Notification", "Permission was already granted") } val calendar = Calendar.getInstance() calendar.set(Calendar.HOUR_OF_DAY, hour) calendar.set(Calendar.MINUTE, minute) calendar.set(Calendar.SECOND, 0) val intent = Intent(this, NotificationReceiver::class.java) val pendingIntent = PendingIntent.getBroadcast(this,Values.ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT) val alarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent) Log.d("Norif", "Notifications set: ${alarmManager.nextAlarmClock}") 

和清单:

 <uses-permission android:name="android.permission.SET_ALARM"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application> <receiver android:name="NotificationReceiver"> <intent-filter> <action android:name="com.tester.alarmmanager"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </receiver> </application>