Search code examples
androidaudioshakevibration

Control vibration of Android


I use this code to set vibration:

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);

I want set vibration only when the sound of phone is enable, but so the shake is always enable.

There is a method to check the sound's state? So i can inhibit the vibration when the sound's state of the phone is set in silent mode.


Solution

  • AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    
    switch (audioManager.getRingerMode()) {
        case AudioManager.RINGER_MODE_SILENT:
            //Logic
            break;
        case AudioManager.RINGER_MODE_VIBRATE:
             //Logic
            break;
        case AudioManager.RINGER_MODE_NORMAL:
             //Logic
            break;
    }
    

    Try this