android audiomanager setRingerMode RINGER_MODE_SILENT does not work

Viewed 231

audiomanager.setRingerMode RINGER_MODE_SILENT has no effect. There is no error.

RINGER_MODE_VIBRATE and RINGER_MODE_NORMAL work fine. In the problematic code, switching from RINGER_MODE_SILENT to RINGER_MODE_VIBRATE produces a switch to vibrate. A previous question suggest resolving this problem by switching to do not disturb mode. https://code-examples.net/en/q/255675d . I implemented this code with no effect.

The relevant android manifest code is:

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

The onStartCommand method of the service is:

 //@Override
 public int onStartCommand(Intent intent, int flags, int startId) {
    Context context = getApplicationContext();
    CharSequence text = "sound mode set silent from sss service!";
    int duration = Toast.LENGTH_LONG;

    NotificationManager notificationManager = 
 (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              
notificationManager.setInterruptionFilter 
(NotificationManager.INTERRUPTION_FILTER_ALARMS);
    }

    try {

        AudioManager myAudioManager = 
    (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    //        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    } catch (Exception e) {
        e.printStackTrace();
    }

    stopSelf();
    return mStartMode;
    }

The only Android device I have available to test this is Android 9, Pie.

How do I set the audio profile in settings to silent programmatically, please?

1 Answers

Try this, I've seen this work for some users:

myAudioManager.setRingerMode(0);

I'm also stuck on this but as far as my research has gone this is an issue with Android 9 specifically. Every device I've tested this snippet on has worked but for some reason android 9 just enables do not disturb mode when below code runs.

myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Related