是否可以从Android O中的NotificationChannel获取声音的名称?

时间:2018-08-02 06:07:42

标签: android

在Android O中,我有以下NotificationChannel

private static String getNotificationChannelId() {
    WeNoteApplication weNoteApplication = WeNoteApplication.instance();

    final String default_notification_channel_id = weNoteApplication.getString(R.string.default_notification_channel_id);

    return default_notification_channel_id;
}

public static String createNotificationChannel() {
    final String default_notification_channel_id = getNotificationChannelId();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return default_notification_channel_id;
    }

    WeNoteApplication weNoteApplication = WeNoteApplication.instance();

    NotificationManager notificationManager =
            (NotificationManager) weNoteApplication.getSystemService(Context.NOTIFICATION_SERVICE);

    final String default_notification_channel_name = weNoteApplication.getString(R.string.default_notification_channel_name);
    final String default_notification_channel_description = weNoteApplication.getString(R.string.default_notification_channel_description);

    NotificationChannel notificationChannel = new NotificationChannel(
            default_notification_channel_id,
            default_notification_channel_name,
            NotificationManager.IMPORTANCE_HIGH
    );

    notificationChannel.setDescription(default_notification_channel_description);
    notificationChannel.enableLights(true);
    notificationChannel.enableVibration(true);

    notificationManager.createNotificationChannel(notificationChannel);

    // How to get the name of sound?
    AudioAttributes audioAttributes = notificationChannel.getAudioAttributes();
    android.util.Log.i("CHEOK", "toString--> " + audioAttributes.toString());
    android.util.Log.i("CHEOK", "getSound--> " + notificationChannel.getSound());
    android.util.Log.i("CHEOK", "getDescription--> " + notificationChannel.getDescription());

    return default_notification_channel_id;
}

我想知道,是否可以从NotificationChannel获取声音的名称,如屏幕截图所示?

enter image description here

1 个答案:

答案 0 :(得分:0)

这就是我所做的。从通知频道获取声音 uri,并使用RingtoneManager对其进行解析。

NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = notificationManager.getNotificationChannel("channel_id");
Uri ringtoneUri = notificationChannel.getSound();
Ringtone r = RingtoneManager.getRingtone(mContext, ringtoneUri);
String nameOfSound = r.getTitle(mContext);