Android: How to Toggle Between Vibrate Settings (Always, Never, Only in Silent Mode, Only When Not in Silent Mode)? Revisited

George Weber picture George Weber · Jul 9, 2012 · Viewed 7.6k times · Source

Issue

Finding methods to toggle between:

  • Always
  • Never
  • Only in Silent Mode
  • Only When Not in Silent Mode

These choices are found by the path --- Menu >> Settings >> Sound >> Vibrate --- on the phone.

It is simple to change by navigation on the phone (by the way, my phone is a Motorola Atrix 2 with Android 2.3.3), but I have yet to come across methods to use in my code.

Code

I basically have buttons that should manipulate the vibrate settings when clicked. One of these buttons is shown here:

    bSilent.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
            audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);

            Toast.makeText(getBaseContext(), "Set to Never", Toast.LENGTH_SHORT).show();
        }
    });

audioManager is defined somewhere above this code as:

final AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

Android offers the AudioManager.setVibrateSetting, but it is now deprecated. Instead, they reference you to the getRingerMode method.

http://developer.android.com/reference/android/media/AudioManager.html

However, using these functions (and any combination of them) do not efficiently move between the four vibrate settings. For example, if I start at "Always", it is seemingly impossible for me to get to "Never". All combinations of vibrate methods will only move between "Always" and "Only in Silent Mode". On the other hand, if I start at "Never", the offered methods will only toggle between "Never" and "Only When Not in Silent Mode".

Therefore, suppose I want to have my phone in silent mode and want it to vibrate. Then, I decide I do not wish it to vibrate any longer. I am unable to switch from "Always" or "Only in Silent Mode" to "Never".

Past Solutions and Posts

I am aware that this is somewhat of a duplicate post on StackOverflow. The issue has been brought up before...

Here: Vibrate settings on Android 2.2

And (more recently) here: Changing vibrate setting

The former of the links provides an "answer". LuTHieR ends up in a discussion and eventually figures out a way on his own. He references the site:

https://android.googlesource.com/platform/packages/apps/Settings/+/froyo-release/src/com/android/settings/SoundSettings.java

and says "I looked at the source code of the com.android.settings.Settings class and copied part of the methods that enable and disable vibrate".

I looked through this site vigorously and could not find what he did. Could anyone clarify his solution?

Question

Does anyone have a way to precisely toggle between "Always", "Never", "Only in Silent Mode", and "Only When Not in Silent Mode"?

Answer

Tapa Save picture Tapa Save · Mar 26, 2014

My solution (path of the function with income String sParam with needed mode of vibration set, refactoring if need to integer 0-3):

AudioManager audioManager = getSystemService( Context.AUDIO_SERVICE);

if( Build.VERSION.SDK_INT < 16)
{
    // sParam may be:
    // 0 - Always
    // 1 - Never
    // 2 - Only in silent mode (when sound is off)
    // 3 - Only when not in silent mode (when sound is on)

    if( (sParam.equals( "1") == true) || (sParam.equals( "3") == true))
    {
        Settings.System.putInt( Static.contextApplication.getContentResolver(), "vibrate_in_silent", 0);
        if( sParam.equals( "1") == true) 
            audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);

        if( sParam.equals( "3") == true)  
            audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
    }

    if( (sParam.equals( "0") == true) || (sParam.equals( "2") == true))
    {
        Settings.System.putInt( Static.contextApplication.getContentResolver(), "vibrate_in_silent", 1);
        if( sParam.equals( "0") == true)  
            audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
        if( sParam.equals( "2") == true)  
            audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ONLY_SILENT);
    }
}
// else (for new SDK > 16 via setRingerMode() ??? )