当我收到通知时,是否可以从原始文件夹开始播放歌曲? 我试过这段代码让这首歌“警觉”开始但没有任何事情发生。
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.beatdanger,
"Your friend is in danger!!!", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri.parse("R.raw.alert");
Intent intent1 = new Intent("wael.ilahi.pfe.SMSRECEIVERESULT");
intent1.putExtra("coordination", str);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent1, 0);
notification.setLatestEventInfo(context, "Help your friend !!",
str, pendingIntent);
notificationManager.notify(0, notification);
答案 0 :(得分:1)
请参阅Android Documentation for Notifications中的“添加声音”章节。
您的问题似乎是,您为notification
- 对象提供了错误的URI:"R.raw.alert"
无效。
相反,如果您需要获取存储在res/raw/
目录中的文件的URI,请使用此模式:"android.resource://" + getPackageName() + "/" + R.raw.your_raw_file
,如下所述:How to play videos in android from assets folder or raw folder?