0

When I open my second activity, I would like it to turn on Do Not Disturb mode on my android device. However, I want Do Not Disturb to only turn on through the second page (either by opening it or through a button created on the second page). The only code I found on stack overflow was in my Android Manifest File

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

I'm not sure where to go from here, any help is appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Lauren L
  • 25
  • 1
  • 6
  • Try this. It requires the permission you're talking about BUT you need the piece of code too. https://stackoverflow.com/a/35324211/1827254 – Eselfar Jul 25 '17 at 14:58

1 Answers1

0

Use that method:

private void setRingerMode(Context context, int mode) {

   NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // Check for DND permissions for API 24+
   if (android.os.Build.VERSION.SDK_INT < 24 || (android.os.Build.VERSION.SDK_INT >= 24 && !nm.isNotificationPolicyAccessGranted())) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(mode);
     }
}

Where mode parameter can be AudioManager.RINGER_MODE_SILENT or AudioManager.RINGER_MODE_NORMAL

Valentun
  • 1,661
  • 12
  • 23
  • Does this only turn on Do Not Disturb once the second page is opened? And this goes into the manifest file, correct? – Lauren L Jul 25 '17 at 21:02
  • @LaurenL, it just a java method you can use. I advice you to put it to separate file, called something like Utils. Then, in Acitvity2 onCreate() call Utils.setRingerMode(this, AudioManager.RINGER_MODE_SILENT); – Valentun Jul 25 '17 at 22:07
  • Do you have an example of the code I have to put in, I am new to coding and no one else in my group project is familiar with it either. I created the separate file, in java and it's text only, I don't know if that is correct. And the onCreate, does that go in the Activity2 java file or under Activity 2 in manifest? – Lauren L Jul 26 '17 at 14:07
  • @LaurenL call this in Acitvity2.java onCreate(). And, it is really does not matter where to put that method. So, if you would use it just in Activity2, you could put it exactly in Activity2. – Valentun Jul 29 '17 at 06:37
  • In my code, the words "context", "NotificationManager","getSystemService(Context", and "isNotificationPolicyAccessGranted" are in red. I am not sure how to fix this issue. – Lauren L Jul 31 '17 at 14:13
  • @LaurenL you should import needed classes. In Android studio you can simply press alt+enter and select first option "import.." – Valentun Aug 05 '17 at 09:33