LuTHieR LuTHieR - 3 years ago 115
Android Question

Vibrate settings on Android 2.2

I'm making a vibrate toggling widget (in fact, its first version is already in the Market) but I'm having some problems with the vibrate settings of Android 2.2.

Up to Android 2.1 I have no problem, when I want to disable vibrate I do


and the same but with
on both lines to turn it on, and it works allright.

However, since Android 2.2 introduced "Vibrate only in silent mode" and "Vibrate when not in silent mode", I don't know how to make it work. When vibrate is set to "Always" or "Never" there's no problem, but as soon as the user manually sets it to one of the other two settings, the app starts doing strange things.

For example, if I have it set to "vibrate only in silent mode" and I turn vibration off via my widget, it turns off, but when I turn it back on... it goes back to "only in silent mode" instead of "Always". In fact, there's no way to set it back to "Always" unless you do it via Settings -> Sound, so I guess the combobox for vibrate mode is doing something else that I don't know of. It's like it stored the value somewhere else, because even if I set manually to "Always", when I disable it and enable it again via the widget (with the code I posted before) it keeps going back to "Vibrate only in silent mode".

And what's more, there's a value in AudioManager class for "Only in silent mode" (VIBRATE_SETTING_ONLY_SILENT) but when you set it to "Only when not in silent mode" it uses VIBRATE_SETTING_ON, so there are no way to detect it (not that I know of, I mean).

Hope I have not been too confusing, but I wanted to give all the possible data. And with all that set...

Does anyone know how to properly handle vibrate settings in 2.2? Is there any other variable that I have to set or function that I have to call in order to disable or enable vibrate completely? Or a way to "reset" the phone to a known state so that it works ok?

The steps to reproduce the problem (or, at least, one of the problems) are:

  • Set vibrate to "only in silent mode" via the settings menu.

  • Set vibrate to "never" via the widget:

    setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);

  • Check in the settings menu that vibrate is set to "never".

  • Set vibrate to "always" via the widget:

    setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);

  • Go to the settings menu -> Vibrate is set to "Only in silent mode"

Answer Source

Ok, I think I finally fixed it.
I looked at the source code of the class and copied part of the methods that enable and disable vibrate:

Thank you anyway magaio, you pointed me in the right direction.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download