如何为 NotificationCompat.Builder 创建的通知添加声音? 我在 res 中创建了一个 raw 文件夹并在那里添加了声音。那么我现在如何将它添加到通知中呢?这是我的通知代码
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
引用声音,因为在 NotificationCompat.Builder
类中有一个明显的方法 - setSound(Uri soundUri)
.
要访问您的 raw
资源,您需要创建 Uri
,如下所示:
android.resource://[PACKAGE_NAME]/[RESOURCE_ID]
所以代码最终可能是这样的:
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
mBuilder.setSound(sound);
关于android - 如何在通知中添加声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14043977/