将通知声音设置为默认闹钟铃声 [英] Set Notification sound as default alarm ringtone
本文介绍了将通知声音设置为默认闹钟铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现的是,如果我将通知声音设置为设备默认的闹钟铃声,如下所示:
What I've discovered is, that if I set notification sound as devices default alarm ringtone like this:
val alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
val builder = NotificationCompat.Builder(
context,
CHANNEL_ID
)
builder.setDefaults(Notification.DEFAULT_VIBRATE or Notification.DEFAULT_LIGHTS)
builder.priority = NotificationCompat.PRIORITY_DEFAULT
builder.setSound(alarmTone)
这几乎适用于所有较旧的设备版本,但是一旦我在 Android 8.0
设备上对其进行测试,它就会将声音设置为默认通知声音.如何获取设备 8.0 的默认闹钟铃声?
This works on almost all older device versions, but as soon as I test it on Android 8.0
device, it sets sound as default notification sound. How can I get default alarm ringtone for devices 8.0?
推荐答案
Oreo 8.0 之后您可能已经创建了播放通知声音的频道.
After 8.0 Oreo You may have created the channel for play notification sound.
private static void initChannels(NotificationManager notificationManager) {
if (Build.VERSION.SDK_INT < 26) {
return;
}
NotificationChannel channel = new NotificationChannel("ID",
"NAME",
NotificationManager.IMPORTANCE_LOW);
channel.setDescription("DESC");
channel.enableVibration(false);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();
channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes);
notificationManager.createNotificationChannel(channel);
}
确保您使用的是 targetSdkVersion 26 或更高版本
Make sure you are using targetSdkVersion 26 or above
这篇关于将通知声音设置为默认闹钟铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文