当我们想通过铃声或者震动提醒用户的时候(类似于手机来电提醒界面),我们需要考虑到手机本身的情景模式。(目前有个OPPO的测试手机就发现,即使调为了静音模式,我依旧可以将铃声播放出来),为了防止“灵异”事件的发生,所以在提示前将情景模式判断以便还是有必要的,特地将代码纪录。
1、获取手机情景模式:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int ringerMode = audioManager.getRingerMode();
其中 ringerMode有三种情况,分别是:AudioManager.RINGER_MODE_NORMAL(铃音模式)、AudioManager.RINGER_MODE_SILENT(静音模式)、AudioManager.RINGER_MODE_VIBRATE(震动模式)
2、获取系统铃声uri:
// 获取系统默认铃声的Uri
private Uri getSystemDefultRingtoneUri() {
return RingtoneManager.getActualDefaultRingtoneUri(this,RingtoneManager.TYPE_RINGTONE);
}
3、播放/停止播放 系统铃声
private void startAlarm() {
//有的手机会创建失败,从而导致mMediaPlayer为空。
mMediaPlayer = MediaPlayer.create(this, getSystemDefultRingtoneUri());
if (mMediaPlayer == null) {//有的手机铃声会创建失败,如果创建失败,播放我们自己的铃声
SoundPoolUtils.playCallWaitingAudio();//自己定义的铃音播放工具类。具体实现见下方
} else {
mMediaPlayer.setLooping(true);// 设置循环
try {
mMediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mMediaPlayer.start();
}
}
private void stopAlarm() {
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
SoundPoolUtils.stopCallWaitingAudio();
}
//SoundPoolUtils类的实现代码
public class SoundPoolUtils {
// 标记是否已经初始化过
private static boolean inited = false;
private static final int MAX_STREAMS = 10;
private static SoundPool soundPool;
// 声音池
private static SparseIntArray soundMap = new SparseIntArray();
public static final int RECORD_PREPARED = 1;
public static final int MSG_SEND = 2;
public static final int CALL_WAITING = 3;
// 正在播放的等待音的ID
private static int CALL_PLAYING_ID = -1;
public static final int GROUP_SWITCH_TIP = 4;
@SuppressWarnings("deprecation")
public static void init(Context context) {
if (context != null) {
if (!inited) {
soundPool = new SoundPool(MAX_STREAMS,
AudioManager.STREAM_MUSIC, 0);
soundMap.append(RECORD_PREPARED,
soundPool.load(context, R.raw.start_record, 1));
soundMap.append(MSG_SEND,
soundPool.load(context, R.raw.msg_send, 1));
soundMap.append(CALL_WAITING,
soundPool.load(context, R.raw.call_waiting, 1));
soundMap.append(GROUP_SWITCH_TIP,
soundPool.load(context, R.raw.group_switch_tip, 1));
inited = true;
}
}
}
public static void playRecordPreAudio() {
soundPool.play(soundMap.get(RECORD_PREPARED), 1, 1, 0, 0, 1);
}
public static void playMessageSendAudio() {
soundPool.play(soundMap.get(MSG_SEND), 1, 1, 0, 0, 1);
}
public static void playCallWaitingAudio() {
CALL_PLAYING_ID = soundPool.play(soundMap.get(CALL_WAITING), 1, 1, 0,
-1, 1);
}
public static void stopCallWaitingAudio() {
soundPool.stop(CALL_PLAYING_ID);
CALL_PLAYING_ID = -1;
}
public static void playGroupSwitchTipAudio() {
soundPool.play(soundMap.get(GROUP_SWITCH_TIP), 1, 1, 0, 0, 1);
}
}
4、开启/关闭震动【不要忘了震动系统权限:<uses-permission Android:name="android.permission.VIBRATE" />】
private void startVibrate() {
if (vibrator == null) {
//获取震动服务
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
//震动模式隔1秒震动1.4秒
long[] pattern = { 1000, 1400 };
//震动重复,从数组的0开始(-1表示不重复)
vibrator.vibrate(pattern, 0);
}
private void stopViberate() {
if (vibrator != null) {
vibrator.cancel();
}
}
5、创建情景模式变化广播接受者监听
// 情景模式切换广播接受者
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 如果是情景模式切换的广播
if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(intent
.getAction())) {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
handleRingMode(am.getRingerMode());//对情景模式做不同的处理
}
}
};
private void handleRingMode(int ringerMode) {
stopAlarm();//关闭铃声
stopViberate();//关闭震动
switch (ringerMode) {
case AudioManager.RINGER_MODE_NORMAL://标准模式(铃声和震动)
startAlarm();
startVibrate();
break;
case AudioManager.RINGER_MODE_SILENT://静音模式
break;
case AudioManager.RINGER_MODE_VIBRATE://震动模式【开启震动不要忘记系统震动权限】
startVibrate();
break;
default:
break;
}
}
private void ringerModeListening() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
}
6、在Activity消亡时取消注册广播接收者
@Override
protected void onDestroy() {
//取消注册广播接收者监听
unregisterReceiver(broadcastReceiver);
super.onDestroy();
}
您可能感兴趣的文章:android开发之蜂鸣提示音和震动提示的实现原理与参考代码Android实现调用震动的方法android滑动解锁震动效果的开启和取消android 触屏的震动响应接口调用方法Android实现手机震动效果Android震动与提示音实现代码android闹铃简单实现Android设置铃声实现代码Android闹铃服务AlarmManager用法深入分析Android编程实现震动与振铃的方法详解