听觉通知(Android奥利奥)
问题描述:
我有现有的应用程序,是能够甚至产生可听通知声音时,该设备有其环体积通过使用Alarm
音频流沉默(假设警报音量不为零)。这在Android 7.1.2听觉通知(Android奥利奥)
工作得很好我的设备升级到奥利奥(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 7.1.1弹出
该解决方案对我来说是下面的行删除
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
...我已经寻找解释为什么删除这个标志的作品,但我还没有发现什么FLAG_AUDIBILITY_ENFORCED呢,除了从开发者文档中描述的任何信息,称
标志定义了系统将确保声音的可听度的行为。