【问题标题】:Set Notification sound as default alarm ringtone将通知声音设置为默认闹钟铃声
【发布时间】:2018-05-26 10:27:52
【问题描述】:

我发现,如果我将通知声音设置为设备的默认闹钟铃声,如下所示:

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 的默认闹钟铃声?

【问题讨论】:

    标签: android notifications alarm


    【解决方案1】:

    8.0 Oreo 之后你可能已经创建了播放通知声音的频道。

    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 或更高版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多