1

I want to change my android device audio mode from Normal to silent and silent to normal. `

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                if(!silent) {
                    audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    silent = true;
                }
                else {
                    audio.setRingerMode( AudioManager.RINGER_MODE_VIBRATE );
                    silent = false;
                }

` This is my code that i'm using. Perfectly running until Android 6 all versions. But on Android 7+ it crashes.

I found a solution on web that opens a settings intent and get some permission from user then this code works fine. It is the code i used then

Intent intent = new Intent(
                    android.provider.Settings
                    .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);

startActivity(intent);

But my project requirement is don't request to open a setting intent to allow it form there. Is should be work like getting user permission to change mode on same activity just like all other permissions i.e. location, read contacts etc.

I tried to find out the find how they implement the permission in the code file but did not find it. Please let me know if anyone know.

Abdur Rahman
  • 894
  • 1
  • 11
  • 27
  • "But on Android 7+ it crashes" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Oct 06 '17 at 16:53
  • Yeah I checked it already. It says you are not granted to change the settings :( – Abdur Rahman Oct 06 '17 at 17:13
  • You may have more luck with getting help if you edit your question and post the entire Java stack trace. – CommonsWare Oct 06 '17 at 17:18

1 Answers1

1

This is the only way to change the Audio Mode of Android with api level > 23

Intent intent = new Intent(
                android.provider.Settings
                .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);

Now officially its released by Android to get user permission to access Notification Policy of device to change the audio Mode.

  • Yeah! OK. Thanks for your response. I was waiting for this confirmation. – Abdur Rahman Nov 04 '17 at 18:41
  • This is not true. Not sure how but Silent Mode Toggle is able to toggle this setting without asking for the permission. I've decompiled the apk and tried to recreate it but I don't understand why it's not possible. – Antonio Vlasic Jan 15 '18 at 14:30