如何在 Android 中设置多个闹钟? [英] How can I setup multiple alarms in Android?
问题描述
到目前为止,多亏了这个网站,我已经能够设置一个闹钟,即使我关掉手机,它也会被设置和激活.
So far and thanks to this website, I've been able to set up an alarm that will be set up and active, even if I turn of my phone.
现在,我设置了一个闹钟来显示事件 A 的提醒,我需要应用程序设置另一个闹钟来显示事件 B 的另一个提醒.
Now, I set up a alarm to show a reminder for event A and I need the application to setup another alarm to show another reminder for event B.
我一定是做错了什么,因为它只会触发事件 A 的提醒.似乎一旦设置,任何其他警报都被理解为相同的警报.:-(
I must be doing something wrong, because it only fires the reminder for event A. It seems that once set up, any other alarm is understood as the same one. :-(
以下是我分两步做的细节:
Here is the detail of what I am doing in two steps:
1) 从一项活动中,我设置了一个闹钟,在特定的时间和日期会呼叫接收者
1) From an activity I set an alarm that at certain time and date will call a receiver
Intent intent = new Intent(Activity_Reminder.this,
AlarmReceiver_SetOnService.class);
intent.putExtra("item_name", prescription
.getItemName());
intent
.putExtra(
"message",
Activity_Reminder.this
.getString(R.string.notif_text));
intent.putExtra("item_id", itemId);
intent.putExtra("activityToTrigg",
"com.companyName.appName.main.Activity_Reminder");
PendingIntent mAlarmSender;
mAlarmSender = PendingIntent.getBroadcast(
Activity_Reminder.this, 0, intent, 0);
long alarmTime = dateMgmt.getTimeForAlarm(pickedDate);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(alarmTime);
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmTime + 15000,
mAlarmSender);
2) 从接收方我调用服务
2) From the receiver I call a service
Bundle bundle = intent.getExtras();
String itemName = bundle.getString("item_name");
String reminderOrAlarmMessage = bundle.getString("message");
String activityToTrigg = bundle.getString("activityToTrigg");
int itemId = Integer.parseInt(bundle.getString("item_id"));
NotificationManager nm = (NotificationManager) context.getSystemService("notification");
CharSequence text = itemName + " "+reminderOrAlarmMessage;
Notification notification = new Notification(R.drawable.icon, text,
System.currentTimeMillis());
Intent newIntent = new Intent();
newIntent.setAction(activityToTrigg);
newIntent.putExtra("item_id", itemId);
CharSequence text1= itemName + " "+reminderOrAlarmMessage;
CharSequence text2= context.getString(R.string.notif_Go_To_Details);
PendingIntent pIntent = PendingIntent.getActivity(context,0, newIntent, 0);
notification.setLatestEventInfo(context, text1, text2, pIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
nm.notify(itemId, notification);
提前致谢,
monn3t
推荐答案
好的,当你设置一个 PendingIntent 时,你应该给它分配一个唯一的 ID,以防你以后想识别它(用于修改/取消它)
Ok, when you set an PendingIntent, you're supposed to assign it a unique ID to it, incase you want to identify it later (for modifying/canceling it)
static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
//Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
//Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().
请求代码就是那个 ID.
The Request code is that ID.
在您的代码中,您不断重置 SAME PendingIntent,而是每次使用不同的 RequestCode.
In your code, you keep resetting the SAME PendingIntent, instead use a different RequestCode each time.
PendingIntent pIntent = PendingIntent.getActivity(context,uniqueRQCODE, newIntent, 0);
它必须是一个整数,我想你有一个primaryid (itemId) 可以从警报B 中识别警报A.
It has to be an integer, i suppose you have a primaryid (itemId) that can identify Alarm A from Alarm B.
这篇关于如何在 Android 中设置多个闹钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!