我的应用程序具有报警功能。我的要求是在闹钟响起时播放自己的声音,但我无法做到这一点。它仅在警报响起时显示通知。我要播放的声音文件位于 Res 的 raw 文件夹中。下面我发布我的代码:
在我的活动课上:
Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
AlarmIntent.putExtra("Ringtone",
Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
(60 * 1000), (24 * 60 * 60 * 1000), Sender);
在接收器类中:
public void onReceive(Context context, Intent intent) {
Intent in = new Intent(context, SnoozeEvent.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
notification.flags = Notification.FLAG_INSISTENT;
notification.sound = (Uri)intent.getParcelableExtra("Ringtone");
manager.notify(1, notification);
}