Create then Cancel multiple alarms set using AlarmManager

I have an application that allows the user to select a pool of minutes. Thus, they can choose 5, 15, 30 and 50 minutes. Then, for each of them, an alarm is set at the same time with the following code (this code block works fine):

//SET ALL ALARMS for(int i = 0; i < spAlarms.length; i++) { try { if(!spAlarms[i].equals("")) { int time = Integer.valueOf(spAlarms[i]); final int intent_id = (int) System.currentTimeMillis(); /* Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, time); */ Intent intent = new Intent(Hop_Timer.this, Alarm_Receiver.class); intent.putExtra("alarm_message", "Time for your " + spAlarms[i] + " min addition!"); intent_ids += "intent_id;"; PendingIntent sender = PendingIntent.getBroadcast(Hop_Timer.this, intent_id, intent, PendingIntent.FLAG_ONE_SHOT); AlarmManager am = (AlarmManager) Hop_Timer.this.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (time*1000), sender); //Toast.makeText(getApplicationContext(), "Set alarm" + String.valueOf(System.currentTimeMillis() + (time*1000)), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } } //ALARMS SET 

I want the user to be able to clear all alarms and start all over again, even after they are installed and hit. I tried the AlarmManager.cancel class, but it does not work. The documentation says that if the intention coincides with the intention set for setting the alarm, it cancels it. So, I use this block of code to cancel it:

 String[] spCANCELS = intent_ids.split(";"); //Cancel all previous set alarms for(int i = 0; i < spCANCELS.length; i++) { try { if(!spCANCELS[i].equals("")) { int time = Integer.valueOf(spCANCELS[i]); Intent intent = new Intent(Hop_Timer.this, Alarm_Receiver.class); intent.putExtra("alarm_message", "Time for your " + spCANCELS[i] + " min addition!"); PendingIntent sender = PendingIntent.getBroadcast(Hop_Timer.this, time, intent, PendingIntent.FLAG_ONE_SHOT); AlarmManager am = (AlarmManager) Hop_Timer.this.getSystemService(Context.ALARM_SERVICE); am.cancel(sender); } } catch (Exception e) { Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } } //All clear! 

Alas, troubles keep coming. I test by adding 10 different alarms at a distance of 5 seconds. I resolve the first three or three alarms and then delete the cancel button, but the alarms continue to come (although all fields are cleared inside the click, so I know that the click listener works).

EDIT: Nevermind, I need to pay more attention when viewing my syntax. This line: aim_ids + = "intent_id;"; should not have a variable aim_id in quotation marks.

+4
source share
1 answer

Nevermind, I need to pay more attention when viewing my syntax. This line: aim_ids + = "intent_id;"; should not have a variable aim_id in quotation marks.

0
source

Source: https://habr.com/ru/post/1341695/


All Articles