Android 强制发出通知声音,即使已静音
我想强制通知中的声音,即使手机处于静音状态。 (是的,我知道,这不是概念,但如果有人闯入我的房子,我希望我的报警系统发出通知) Iam在Android 6.0和HTC one A9上使用Android API 23。 据我所知,我唯一的选择是:Android 强制发出通知声音,即使已静音,android,android-notifications,Android,Android Notifications,我想强制通知中的声音,即使手机处于静音状态。 (是的,我知道,这不是概念,但如果有人闯入我的房子,我希望我的报警系统发出通知) Iam在Android 6.0和HTC one A9上使用Android API 23。 据我所知,我唯一的选择是: 缓存当前音频设置 更改为振铃器\模式\正常并设置最大音频 运行通知 恢复旧的音频设置 如果我在调试模式下做一个断点并遍历代码的每一行,这就可以了。 但是如果我在调试模式下运行(一切都一样)而没有断点,它就不起作用了。因此,我认为这是一个时间问题,音频管理
// save current audio settings to restore later
int audio_ringermode = audio_mode.getRingerMode();
audio_old_volume =
audio_mode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
// set new audio settings
audio_mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audio_mode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,audio_mode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
// Notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_home_notify)
.setColor(notification_icon)
.setContentTitle(notification_title)
.setContentText(notification_text)
.setAutoCancel(true)
.setSound(defaultSoundUri);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId,notificationBuilder.build());
// restore old audio settings
audio_mode.setRingerMode(audio_ringermode);
audio_mode.setStreamVolume(AudioManager.STREAM_NOTIFICATION, audio_old_volume ,0);
即使手机静音,这也适用于我:
private void playSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(mContext, R.raw.ring_message);
AudioManager am =
(AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mediaPlayer.start();
}
我认为notificationManager.notify不是同步的。也许你需要设定一个更好的?。确保您已在清单中授予权限。