[英]More than one alarms in alarmManager
This is an sms timer app so i need to set multiple alarms using alarm manager so that i can send messages to many people. 这是一个短信计时器应用程序,因此我需要使用警报管理器设置多个警报,以便我可以向许多人发送消息。 my code is : 我的代码是:
Phone = phone.getText().toString();
Message = message.getText().toString();
phone.setText("");
message.setText("");
//Validating if any field empty
if (Phone.length() >= 10 && Message.length() > 0) {
Toast.makeText(this.getApplicationContext(), "Your sms will be sent soon", Toast.LENGTH_SHORT).show();
//Getting Calender Reference
Calendar cal = Calendar.getInstance();
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
cal.set(Calendar.MINUTE, time_picker.getMinute());
cal.set(Calendar.HOUR_OF_DAY, time_picker.getHour());
} else {
//Setting the date and time from the time picker
cal.set(Calendar.MINUTE, time_picker.getCurrentMinute());
cal.set(Calendar.HOUR_OF_DAY, time_picker.getCurrentHour());
}
int a = (Calendar.getInstance().get(Calendar.SECOND) * 1000);
myIntent = new Intent(this, MyReceiver.class);
//Pending Intent for sending the intent afterwards
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0);
alarmManager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
alarmManager.set(AlarmManager.RTC, cal.getTimeInMillis() - a, pendingIntent);
All the code in in an onclick function, so when the user fills the data and clicks send it should set an alarm. 所有代码都包含在onclick函数中,因此,当用户填充数据并单击发送时,应设置一个警报。 I only need at most 5 alarms. 我最多只需要5个警报。 I tries creating AlarmManager[]
and ArrayList<PendingIntent>
but it didnt work, only the latest alarm is getting set. 我尝试创建AlarmManager[]
和ArrayList<PendingIntent>
但没有成功,仅设置了最新的警报。
PendingIntents are usually difrenciated by their requestCode (works like an ID), that may be what's causing your problem, you can set the requestCode like this: PendingIntent通常由它们的requestCode(像ID一样起作用)来区分,这可能是导致您出现问题的原因,您可以这样设置requestCode:
Add this outside of onCreate: 将其添加到onCreate之外:
static int i;
and replace this: 并替换为:
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0);
with this: 有了这个:
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), i++ /*this number is the PendingIntent's requestCode*/, myIntent, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.