17

I used AudioManager.setRingerMode() to handle the device volume in my application using:

  • AudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT) to turn off vibration and sound.
  • AudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE) to turn off the sound and turn on the vibration.
  • AudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL) to turn on the sound and turn on the vibration.

In other words, I was able to get control of the device volume and reach all "modes".

But, it's not possible to handle all modes of device volume in Android 5.0 using only the AudioManager.setRingerMode.

As per the documentation:

Setting the device to RINGER_MODE_SILENT causes the device to enter the new priority mode. The device leaves priority mode if you set it to RINGER_MODE_NORMAL or RINGER_MODE_VIBRATE.

How can I handle all modes (none, vibration in priority, sound in priority, vibration in all, sound in all) of device volume in Android 5.0?

Eldoth
  • 275
  • 2
  • 8

1 Answers1

0

With the new 5.0 API's there are a bunch of methods that can be used for setting the ringer/vibrate.

setVibrate (long[] pattern)

This method controls the vibration of the device. Takes a long in which the first value indicates the number of milliseconds to wait before turning on the vibrator. More details:

http://developer.android.com/reference/android/os/Vibrator.html#vibrate(long[], int)

http://developer.android.com/reference/android/app/Notification.Builder.html#setVibrate(long[])

setDefaults (int defaults)

This one sets the notifications properties i.e. SOUND,VIBRATE,ALL etc etc.

From the docs:

The value should be one or more of the following fields combined with bitwise-or: DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.

http://developer.android.com/reference/android/app/Notification.Builder.html#setDefaults (int defaults)

Hope this helps.

user2511882
  • 9,022
  • 10
  • 51
  • 59
  • I'm not trying to send a sound notification or a vibration. The question is about the sound control of the device. In a few words, how can I set the sound control of the device to "none" ? – Eldoth Jun 25 '15 at 03:24
  • http://developer.android.com/reference/android/media/AudioManager.html#shouldVibrate(int).. check the first line in the link..Google recommends each app handling its own sound/vibrate instead of trying to control the device volume itself.. – user2511882 Jun 25 '15 at 21:48
  • Ok, I agree that it's not a good practice try to control the device volume. But, suppose that I want to try to control this. Is there any way? – Eldoth Jun 26 '15 at 04:25
  • Ok, firstly the OP mentioned about handling notifcation in the applications. The link provided in the OP also pointed to notification behavior changes in 5.0. If you are concerned about handlling notification sounds from your app the answer should work fine. In case if you are talking about controlling the sound of the device, you can still use the RingerMode class. http://developer.android.com/reference/android/media/AudioManager.html#setRingerMode(int). The OP needs to be updated i guess. – user2511882 Jun 26 '15 at 17:54
  • Oh, sorry. By notification, I wanted to mean the device volume. Your answer would be right if I was trying to set the ringer/vibrate of my app. With RingerMode class, I can't reach the state "None" of the volume device. Is there any way to reach this state? – Eldoth Jun 26 '15 at 18:52