如何在 Android 中设置多个闹钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3273342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I setup multiple alarms in Android?
提问by monn3t
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.
到目前为止,多亏了这个网站,我已经能够设置一个警报,即使我关掉手机,它也会被设置和激活。
Now, I set up a alarm to show a reminder for event A and I need the application to setup another alarm to show anotherreminder for event B.
现在,我设置了一个闹钟来显示事件 A 的提醒,我需要应用程序设置另一个闹钟来显示另一个事件 B 的提醒。
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. :-(
我一定是做错了什么,因为它只会触发事件 A 的提醒。似乎一旦设置,任何其他警报都被理解为相同的警报。:-(
Here is the detail of what I am doing in two steps:
这是我分两步做的详细信息:
1) From an activity I set an alarm that at certain time and date will call a receiver
1)从活动中我设置了一个警报,在特定的时间和日期将呼叫接收器
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) From the receiver I call a service
2)从接收器我调用服务
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);
Thanks in Advance,
提前致谢,
monn3t
monn3t
回答by st0le
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)
好的,当您设置 PendingIntent 时,您应该为其分配一个唯一的 ID,以防您以后想识别它(用于修改/取消它)
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().
The Request code is that ID.
请求代码就是那个 ID。
In your code, you keep resetting the SAMEPendingIntent, instead use a different RequestCode each time.
在您的代码中,您不断重置SAMEPendingIntent,而不是每次使用不同的 RequestCode。
PendingIntent pIntent = PendingIntent.getActivity(context,uniqueRQCODE, newIntent, 0);
It has to be an integer, i suppose you have a primaryid (itemId) that can identify Alarm A from Alarm B.
它必须是一个整数,我想你有一个可以从警报 B 中识别警报 A的主 ID ( itemId)。
回答by Junaid
You can set up multiple alarms by supplying different request code in pendingIntent.getBroadcast(......)
您可以通过在 pendingIntent.getBroadcast(......) 中提供不同的请求代码来设置多个警报
The approach which I used to setup multiple alarm is that I created a single alarm. I initialized a static integer in alarm setting class which will be incremented each time from my main activity whenever I click on "add alarm" button in my main activity. E.g.
我用来设置多个警报的方法是我创建了一个警报。我在警报设置类中初始化了一个静态整数,每当我单击主活动中的“添加警报”按钮时,它都会从我的主要活动中每次递增。例如
MainActivity.java
主活动.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addAlarmClick(View v) {
AlarmActivity.broadcastCode++;
startActivity(new Intent(this, AlarmActivity.class));
}
}
AlarmActivity.java
报警活动.java
public class AlarmActivity extends AppCompatActivity {`
//........
public static int broadcastCode=0;
//........
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this,
broadcastCode, myIntent, 0);