铃声音量静音时使用报警流的声音通知(Android Oreo)
我有一个现有的应用程序,通过使用铃声音量静音时使用报警流的声音通知(Android Oreo),android,android-notifications,alarm,android-8.0-oreo,Android,Android Notifications,Alarm,Android 8.0 Oreo,我有一个现有的应用程序,通过使用报警音频流(假设报警音量为非零),即使当设备的铃声静音时,也能发出声音通知。这在安卓7.1.2上运行良好 将我的设备升级到Oreo(8.0.0)后,将不再听到通知,除非铃声音量不为零。应用程序保持不变 因此,我试图解决这个问题,是针对Oreo(SDK 26)API重新编译,并包含新的NotificationChannel内容,看看这是否有帮助,但实际上没有 请注意,此代码是从服务中执行的 以下是代码摘录: int importance = Notification
报警
音频流(假设报警音量为非零),即使当设备的铃声静音时,也能发出声音通知。这在安卓7.1.2上运行良好
将我的设备升级到Oreo(8.0.0)后,将不再听到通知,除非铃声音量不为零。应用程序保持不变
因此,我试图解决这个问题,是针对Oreo(SDK 26)API重新编译,并包含新的NotificationChannel
内容,看看这是否有帮助,但实际上没有
请注意,此代码是从服务中执行的
以下是代码摘录:
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();
我也遇到过类似的问题,但它在安卓7.1.1上突然出现
对我来说,解决办法是删除以下行
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
我已经搜索了为什么删除这个标志有效的解释,但是除了开发者文档中的描述之外,我没有找到任何关于flag_AUDIBILITY_ENFORCED的功能的信息,比如
定义系统将确保声音可听性的行为的标志