【发布时间】:2014-10-14 05:40:19
【问题描述】:
我正在使用 Android 的 NotificationManager 创建通知。
是否可以“覆盖”手机的音量(静音)设置,从而始终播放通知的声音?
我需要这个的原因如下: 通知非常重要,仅靠振动可能还不够。必须提醒用户。因此,即使手机静音或音量很低,也会播放声音。
【问题讨论】:
标签: android notifications android-notifications
我正在使用 Android 的 NotificationManager 创建通知。
是否可以“覆盖”手机的音量(静音)设置,从而始终播放通知的声音?
我需要这个的原因如下: 通知非常重要,仅靠振动可能还不够。必须提醒用户。因此,即使手机静音或音量很低,也会播放声音。
【问题讨论】:
标签: android notifications android-notifications
是的,这是可能的,
MediaPlayer mMediaPlayer;
Uri notification = null;
notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(ctx, notification);
// mMediaPlayer = MediaPlayer.create(ctx, notification);
final AudioManager audioManager = (AudioManager) ctx
.getSystemService(Context.AUDIO_SERVICE);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
// mMediaPlayer.start();
mMediaPlayer.setLooping(true);
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
mMediaPlayer.seekTo(0);
mMediaPlayer.start();
}
});
【讨论】:
您可以像这样将 RINGING 模式从静音更改为正常
AudioManager mobilemode = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mobilemode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
// Turn on all sound
// turn on sound, enable notifications
mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, false);
//notifications
mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
//alarm
mobilemode.setStreamMute(AudioManager.STREAM_ALARM, false);
//ringer
mobilemode.setStreamMute(AudioManager.STREAM_RING, false);
//media
mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, false);
// Turn off all sound
// turn off sound, disable notifications
mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, true);
//notifications
mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//alarm
mobilemode.setStreamMute(AudioManager.STREAM_ALARM, true);
//ringer
mobilemode.setStreamMute(AudioManager.STREAM_RING, true);
//media
mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, true);
对于您的情况,您可以尝试这样的方法
int previousNotificationVolume =mobilemode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,mobilemode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
// Play notification sound
// Set notification sound to its previous
mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,previousNotificationVolume, 0);
【讨论】: