[英]More than one alarms in alarmManager
这是一个短信计时器应用程序,因此我需要使用警报管理器设置多个警报,以便我可以向许多人发送消息。 我的代码是:
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);
所有代码都包含在onclick函数中,因此,当用户填充数据并单击发送时,应设置一个警报。 我最多只需要5个警报。 我尝试创建AlarmManager[]
和ArrayList<PendingIntent>
但没有成功,仅设置了最新的警报。
PendingIntent通常由它们的requestCode(像ID一样起作用)来区分,这可能是导致您出现问题的原因,您可以这样设置requestCode:
将其添加到onCreate之外:
static int i;
并替换为:
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0);
有了这个:
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), i++ /*this number is the PendingIntent's requestCode*/, myIntent, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.