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?