I have a custom class SettingsPreferences
where I handle the changing of a few settings for my Voice Recorder app. At the moment, I am trying to handle turning on and off Do Not Disturb mode.
I've created a method requestAccessNotificationPermission
that is called by another method, checkIfNotifGranted
, that checks if Notification Policy Access is granted.
@RequiresApi(api = Build.VERSION_CODES.M)
void checkIfNotifGranted() {
if (!notificationManager.isNotificationPolicyAccessGranted()) {
requestAccessNotificationPermission();
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestAccessNotificationPermission(Activity activity) {
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
activity.startActivity(intent);
}
My plan is that if this would work, I would then use the two methods below to handle the turning off and on of do not disturb mode.
@RequiresApi(api = Build.VERSION_CODES.M)
void doNotDisturbOff() {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
}
@RequiresApi(api = Build.VERSION_CODES.M)
void doNotDisturbOn() {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
However, I do not know how to deal with this Activity
problem. I tried to put this
as an argument for the call to requestAccessNotificationPermission
but it does not work.
I can't have these methods in my SettingsFragment
as I can not call a non static method from a static context. Therefore, I have the code below to call the methods in my custom class.
final Preference dnd = findPreference("doNotDisturb");
assert dnd != null;
dnd.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String boolCheck = newValue.toString();
if (boolCheck.equals("true")) {
new SettingsPreferences().checkIfNotifGranted();
new SettingsPreferences().doNotDisturbOn();
}
else if (boolCheck.equals("false")) {
new SettingsPreferences().checkIfNotifGranted();
new SettingsPreferences().doNotDisturbOff();
}
return false;
}
});
Any help or explanation would be greatly appreciated as I'm really stumped with this one.
Thank you.