问题
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
am.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,AudioManager.VIBRATE_SETTING_OFF);
am.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,AudioManager.VIBRATE_SETTING_OFF);
and the same but with VIBRATE_SETTING_ON
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"
回答1:
I had to mess with these settings myself when some application messed up my Nexus One's individual settings. Here they are:
Always vibrate on ring:
setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
Never vibrate on ring:
setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
Vibrate on ring in silent only:
setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ONLY_SILENT);
Always vibrate on notify:
setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
Never vibrate on notify:
setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
Vibrate on notify in silent only:
setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ONLY_SILENT);
Change ringer mode:
AudioManager.setRingerMode(RINGER_MODE_NORMAL); AudioManager.setRingerMode(RINGER_MODE_SILENT); AudioManager.setRingerMode(RINGER_MODE_VIBRATE);
Vibrate settings are independent of ringer settings. Confusion ensues.
回答2:
Ok, I think I finally fixed it.
I looked at the source code of the com.android.settings.Settings
class and copied part of the methods that enable and disable vibrate:
https://android.googlesource.com/platform/packages/apps/Settings/+/master/src/com/android/settings/SoundSettings.java
Thank you anyway magaio, you pointed me in the right direction.
来源:https://stackoverflow.com/questions/3742323/vibrate-settings-on-android-2-2