gpt4 book ai didi

android - 铃声音量静音时使用警报流的声音通知 (Android Oreo)

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

我有一个现有的应用程序,即使设备通过使用 Alarm 音频流将其铃声音量静音(假设 Alarm音量不为零)。这在 Android 7.1.2 上运行良好

将我的设备升级到 Oreo (8.0.0) 后,除非铃声音量不为零,否则通知不再可听见应用没有变化

所以我尝试解决这个问题是针对 Oreo (SDK 26) API 重新编译并包含新的 NotificationChannel 东西只是为了看看是否有帮助,但没有帮助。

请注意,此代码是从 Service 中执行的。

这是一段代码摘录:

int importance = NotificationManager.IMPORTANCE_MAX;
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

NotificationChannel mChannel = new NotificationChannel("channel1", "Alarms", importance);
mChannel.setDescription("Alarms that alert you immediately");
mChannel.enableLights(true);

mChannel.enableVibration(true);
mChannel.setSound(defaultSoundUri,
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
);

notificationManager.createNotificationChannel(mChannel);

Notification.Builder notificationBuilder = new Notification.Builder(this,"channel1")
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("Alarm Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri, new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build())
.setContentIntent(pendingIntent)
.setCategory(Notification.CATEGORY_ALARM)
.setPriority(Notification.PRIORITY_MAX);


Notification n = notificationBuilder.build();
n.flags = n.flags | Notification.FLAG_INSISTENT;
notificationManager.notify(0 /* ID of notification */, n);

有什么想法吗?

最佳答案

我也为这个问题苦苦挣扎。我还没有找到为什么会这样,但我使用 MediaPlayer 解决了这个问题。我删除了将声音添加到通知的部分,并将该部分替换为:

        Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this.getApplicationContext(), ringtoneUri);
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_ALARM).build());
mediaPlayer.setLooping(false);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public final void onCompletion(MediaPlayer it) {
mediaPlayer.stop();
mediaPlayer.release();
}
});
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public final void onPrepared(MediaPlayer it) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();

关于android - 铃声音量静音时使用警报流的声音通知 (Android Oreo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137469/

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