https://static.06ms.com/static/images/index/16.jpg

安卓来电静音

来电铃声响起后,可通过此代码实现静音而非挂断。

private static int previousMuteMode = -1;

/**
 * 来电静音
 * 
 * @param context
 */
private void toggleRingerMute(Context context)
{
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (previousMuteMode == -1)
    {
        previousMuteMode = am.getRingerMode();
        am.setRingerMode(0);
    }
    am.setRingerMode(previousMuteMode);
    previousMuteMode = -1;
}

https://static.06ms.com/static/images/index/19.jpg

安卓通话免提切换

实现免提开启和关闭,需要添加的权限

/**
 * 免提
 * 
 * @param context
 */
private void toggleSpeaker(Context context)
{
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    am.setMode(AudioManager.MODE_IN_CALL);
    am.setSpeakerphoneOn(!am.isSpeakerphoneOn());
}

https://static.06ms.com/static/images/index/11.jpg

Android铃声代码片段

播放默认铃声
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0)
{
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
player.start();
}

设置默认来电铃声