我的应用程序有报警功能。我的要求是在闹钟响起时播放自己的声音,但我无法做到这一点。它只在闹钟响起时显示通知。我想要播放的声音文件在Res.下面的raw文件夹中,我正在发布我的代码:
在我的activity类中:
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);
在receiver类中:
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);
}
发布于 2012-08-13 15:42:10
您可以设置自定义声音在您的捆绑或进入SD Card.only设置路径的声音文件作为一个通知sound.use下面的代码。
notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
发布于 2012-08-13 15:50:36
尝试更改这两行:
//In your activity:
AlarmIntent.putExtra("Ringtone",getResources().getResourceName(R.raw.shankh_final_mid));
....
//In your reciever
notification.sound = (Uri)(Uri.parse(intent.getStringExtra("Ringtone")));
发布于 2012-08-13 15:37:07
尝试使用此代码来发出您自己的通知:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note = new Notification(R.drawable.icon, "Your Text to Show", System.currentTimeMillis());
note.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
int nid = 123456789;
manager.notify(nid, note);
https://stackoverflow.com/questions/11929929
复制相似问题