gpt4 book ai didi

android - Android:通知声音持续时间

转载 作者:行者123 更新时间:2023-12-03 00:49:48 26 4
gpt4 key购买 nike

尝试设置通知警报声音的持续时间时遇到问题。现在是这样的:

NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mBuilder.setAutoCancel(true);

Uri alarmSound = Uri.parse(sharedPref.getString(
"notifications_new_message_ringtone", ""));

if (alarmSound != null) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
try {
mediaPlayer.setDataSource(context, alarmSound);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {

}

mBuilder.setSound(alarmSound);



mNotificationManager.notify(notificationId, mBuilder.build());

new Thread() {
public void run() {

try {
sleep(sharedPref.getInt("alarm_duration",2000) * DateUtils.SECOND_IN_MILLIS);
} catch (InterruptedException e) {
e.printStackTrace();
}
mNotificationManager.cancel(notificationId);
}
}.start();
}

如您所见,我在sharedPref变量给定的一段时间后停止了通知(用户可以进行设置)。关键是这一行:“mNotificationManager.cancel(notificationId)”不仅会停止声音,还会停止整个通知(灯光,振动,甚至是通知抽屉上的图标)。

有没有办法只关闭通知而不完全取消通知?

提前致谢!。

最佳答案

没有办法只能取消声音。您可以取消整个Notification,然后在没有声音的情况下再次发布。或者,您可以将AudioManager.STREAM_NOTIFICATION流类型静音x时间,然后在Notification声音结束后将其取消静音。

要控制音量:

    setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

静音 Notification声音:
    final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true or false);

关于android - Android:通知声音持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22664539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com