如何添加声音通知? [英] How to add sound to notification?
问题描述
你如何添加声音通过NotificationCompat.Builder创建的通知? 我在水库的RAW文件夹,并添加的声音出现。那么,如何我现在把它添加到通知?这是我的通知code
INT NOTIFY_ID = 100;
意图notificationIntent =新的意图(这一点,Notification.class);
PendingIntent pendingIntent = PendingIntent.getActivity(此,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder =
新NotificationCompat.Builder(本)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.notification)
.setContentTitle(警告)
.setContentText(救命!)
NotificationManager经理=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID,mBuilder.build());
我在这里猜测的问题是如何引用的声音与乌里
,因为有在一个明显的方法 NotificationCompat.Builder
类 - setSound(URI soundUri)
要访问您的原
的资源,你需要创建乌里
如下:
android.resource:// [PACKAGE_NAME] / [RESOURCE_ID]
所以,code最终可能会找这样的:
开放的声音= Uri.parse(android.resource://+ getPackageName()+/+ R.raw.notifysnd);
mBuilder.setSound(音);
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());
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)
.
To access your raw
resources you need to create the Uri
as follows:
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屋!