简体   繁体   中英

How to check if phone is mute, vibrate or loud in android

This is my code to mute/unmute phone. But I would like to check if phone is alreay on mute and if is not then i will mute it. If is in vibrate then i will make it normal.

Can i check this states somehow?

public void changeRingerMode(Context context){

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    /**
    * To Enable silent mode.....
    */
    audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);

    /**
    * To Enable Ringer mode.....
    */
    audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

}

try this:

switch( audio.getRingerMode() ){
case AudioManager.RINGER_MODE_NORMAL:
   break;
case AudioManager.RINGER_MODE_SILENT:
   break;
case AudioManager.RINGER_MODE_VIBRATE:
   break;
}

In my case I found out that even if my phone settings is to vibrate, it will return AudioManager.RINGER_MODE_SILENT when is it silence, so I found a way to check the vibration mode if then:

public static boolean checkVibrationIsOn(Context context){
    boolean status = false;
    AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    if(am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE){
        status = true;
    } else if (1 == Settings.System.getInt(context.getContentResolver(), "vibrate_when_ringing", 0)) //vibrate on
        status = true;
    return status;
}

And to get the whole picture, to find out if ringer is on:

public static boolean checkRingerIsOn(Context context){
    AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM