Search code examples
androidandroid-permissions

Android: Detect the instance when DO NOT DISTURB permission has been granted


I have a use-case where in I need to detect the instance when DO NOT DISTURB permission has been granted by the user to my app. I have a service running in the foreground, so I can use this service to detect when the permission is granted.

I am redirecting the user to DO NOT DISTURB settings as follows and this is working as expected:

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();

Is there a CallBack method or a Reciver that I can make use of to detect the instance when DO NOT DISTURB has been granted to my app?


Solution

  • After browsing through the official documentation and a large number of other references, it has dawned on me that there is no callback to detect the instance when there is change in the permission status for an app.

    PS: It is possible to find if a specific permission has been granted or not but not the instance at which it was granted.