0

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.

olsxn
  • 27
  • 7

2 Answers2

3

You can't use this from inside an anonymous inner class. You will have to use MainActivity.this

Alan Deep
  • 2,037
  • 1
  • 14
  • 22
  • Thanks for the reply. Passing that into my requestAccessNotificationPermission method throws the error "MainActivity is not an enclosing class" – olsxn May 08 '20 at 12:40
  • 1
    Ah okay, because you're calling MainActivity.this from the fragment. So just use getActivity() instead of MainActivity.this. See why you should post your entire code here? Because it differs from a situation to another. – Alan Deep May 08 '20 at 12:45
  • Yeah I didn't want to post hundreds of lines in case it put anyone off the question. So what I've done now is just put the code from requestAccessNotificationPermission into the if statment in checkIfNotifGranted. Now my problem is when calling that method from my Settings fragment I can't pass Settings.this as an argument as it cannot be referenced from a static context. – olsxn May 08 '20 at 12:59
  • Do you think I could do something similar to this? https://stackoverflow.com/questions/2002288/static-way-to-get-context-in-android But for Activity rather than Context? – olsxn May 08 '20 at 13:00
  • 1
    Yes you can, but it's a good practice to pass the activity context because SettingsFragment belongs to the activity context. But ok get the context from Application.. however it's not recommended for testing. – Alan Deep May 08 '20 at 13:01
  • Thanks Alan. I will have to do a bit more research and learn more about it as I'm just learning as I go at the moment. Appreciate all the help thanks for taking the time. – olsxn May 08 '20 at 13:05
0

You can pass create Application class and pass the MyApplication.getInstance() to your methos.

If you are using kotlin then simple set applicationContext.

Hope it may help.

Komal
  • 328
  • 3
  • 15