Android mute/unmute phone

我只是一个虾纸丫 提交于 2019-11-27 19:24:39

This is the permission for vibrate into the manifest file

<uses-permission android:name="android.permission.VIBRATE" />

this is for to put the device in silent mode with vibrate

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

this is for to put into the ringing mode

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);

audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
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);

}
leeCoder

If what you want is to disable sound and restore the sound setting to previous state, this worked for me.

static int ringstate = 0;
private void soundOn(boolean off){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
   if(off)
    {   //turn off ringing/sound
     //get the current ringer mode
     ringstate = audio.getRingerMode();
     if(ringstate!=AudioManager.RINGER_MODE_SILENT)
      audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);//turn off
    }
  else
  {
    //restore previous state
    audio.setRingerMode(ringstate);


  }

}

This should do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!