alarmManager 中有多个报警
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>
但它没有用,只有最新的警报正在设置。
PendingIntents 通常由它们的 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);
这是一个短信定时器应用程序,所以我需要使用闹钟管理器设置多个闹钟,以便我可以向很多人发送消息。 我的代码是:
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>
但它没有用,只有最新的警报正在设置。
PendingIntents 通常由它们的 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);