gpt4 book ai didi

android - NotificationChannel 通知作为电话静音时的警报(奥利奥)

转载 作者:行者123 更新时间:2023-11-29 02:28:41 25 4
gpt4 key购买 nike

我将 NotificationCompat.Builder 设置为:

.setSound(getNotificationSound(), AudioManager.STREAM_ALARM)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_MAX)

在其他强制属性中。

对于我正在使用的 NotificationChannel,我添加了:

.setBypassDnd(true)

对于奥利奥来说,问题在于:

  • 当手机处于静音或振动状态时,触发的通知没有任何声音,因此它不像闹钟那样起作用

通知类别/ channel 中的请勿打扰自定义异常切换按钮有什么意义?它是否有助于实现我的目标,因为我没有看到任何差异?

对于早于 Oreo 的版本,在我不使用 NotificationChannel 的情况下,我有一个我更喜欢的行为:

  • 手机静音但没有振动时通知声音有效
  • 通知声音和振动在手机振动或启用声音时有效

有什么办法可以解决这种不一致的问题吗?

最佳答案

最后我放弃了使用声音和振动的通知 channel 来获得跨 Android 版本的一致结果。

channel.setSound(null, null);

并使用 MediaPlayerVibrator 代替辅助类,如下所示:

public class RingtoneAndVibrationPlayer extends ContextWrapper{

private MediaPlayer mMediaPlayer;
private Vibrator mVibrator;

public RingtoneAndVibrationPlayer(Context context) {
super(context);
}

public void play() {
try {
mMediaPlayer = new MediaPlayer();
mVibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

final Uri uri = Uri.parse(PreferenceHelper.getNotificationSound();

mMediaPlayer.setDataSource(this, uri);
if (PreferenceHelper.isRingtoneEnabled()) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(PreferenceHelper.isRingtoneInsistent());
mMediaPlayer.prepareAsync();
}

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});

if (PreferenceHelper.isVibrationEnabled()) {
mVibrator.vibrate(new long[] {0, 500, 500, 500},
PreferenceHelper.isRingtoneInsistent() ? 2 : -1);
}
} catch (SecurityException | IOException e) {
stop();
}
}

public void stop() {
if (mMediaPlayer != null && mVibrator != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
}
if (mVibrator != null) {
mVibrator.cancel();
}
}
}

我看到的唯一缺点是用户可以手动更改将与上述 channel 一起播放的通知 channel 的声音和振动设置。就我而言,在应用程序设置中对声音和振动有明确的偏好是不鼓励的。

关于android - NotificationChannel 通知作为电话静音时的警报(奥利奥),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50785055/

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