如何在通知中添加声音? [英] How to add sound to notification?

查看:35
本文介绍了如何在通知中添加声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向 NotificationCompat.Builder 创建的通知添加声音?我在 res 中创建了一个原始文件夹并在那里添加了声音.那么我现在如何将其添加到通知中?这是我的通知代码

How do you add sound to a notification created by NotificationCompat.Builder? I created a raw folder in res and added the sound there. So how do I now add it to notification? This is my Notification code

    int NOTIFY_ID=100;
    Intent notificationIntent = new Intent(this, Notification.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle("Warning")
            .setContentText("Help!")

    NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mgr.notify(NOTIFY_ID, mBuilder.build());

推荐答案

我猜这里的问题是如何使用 Uri 引用声音,因为 Uri 中有一个明显的方法code>NotificationCompat.Builder 类 - setSound(Uri soundUri).

I'm guessing the problem here is how to reference the sound with a Uri, as there is an obvious method in the NotificationCompat.Builder class - setSound(Uri soundUri).

要访问您的 raw 资源,您需要按如下方式创建 Uri:

To access your raw resources you need to create the Uri as follows:

android.resource://[PACKAGE_NAME]/[RESOURCE_ID]

android.resource://[PACKAGE_NAME]/[RESOURCE_ID]

所以代码最终可能看起来像这样:

So the code could end up looking like that:

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
mBuilder.setSound(sound);

这篇关于如何在通知中添加声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆