Я использовал startActivityForResult
вместо startActivity
следующим образом:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
//Permission not granted so we will ask for it.
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult(intent, 55);
} else {
//Permission was granted
}
, затем я проверил результат в onActivityResult
, используя isNotificationPolicyAccessGranted()
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 55) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NotificationManager mNotificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
assert mNotificationManager != null;
if (mNotificationManager.isNotificationPolicyAccessGranted()) {
//DND permission is granted
//do something
}else{
//DND permission isn't granted
//request the permission again or do something else
}
}
}
}
это сработало для меня, но я не знаю, есть ли лучший способ.