安卓:在授予"请勿打扰"权限时检测实例



我有一个用例,我需要检测用户向我的应用程序授予请勿打扰权限的实例。我有一个服务在前台运行,因此我可以使用此服务来检测何时授予权限。

我正在将用户重定向到"请勿打扰"设置,如下所示,并且按预期工作:

NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
builder.setMessage(context.getString(R.string.don_not_disturb_msg))
.setCancelable(false)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
context.startActivity(intent);
}
dialog.dismiss();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();

是否有回方法或接收器可用于检测实例何时向我的应用授予"请勿打扰"?

在浏览了官方文档和大量其他参考资料后,我突然意识到,当应用程序的权限状态发生变化时,没有回调来检测实例。

附注: 可以查找是否已授予特定权限,但无法查找授予该权限的实例。

最新更新