Reputation: 23
I developed an Alarm clock. A main screen witch lists all alarms and add option to add new alarm to list. I am using an Alarm Manager to fire a notification or open user screen. After user open notification screen and hit button, it returns to main screen and list all alarms.
All alarms that already fired, and will not fire again, are marked with different color. To identify witch alarms are fired, I am using this code:
How to check if AlarmManager already has an alarm set?
But this code is returning false (alarm not set) for alarm only after 30 seconds from fired alarm and returns true (alarm set) immediately when i return to main screen.
To set alarm I am using this service:
protected static void SetBackgroudAlrm(long alarmTime, boolean toggleBtnRep,int AlrmID,Context context) {
/** Set Alarm in Background */
AlarmManager manager;
PendingIntent pIntent = null ;
Intent alarmIntent = new Intent(context,AlarmReceiver.class);
pIntent = PendingIntent.getBroadcast(context, AlrmID, alarmIntent, 0);
manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if (toggleBtnRep){ //repeat is on
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,alarmTime ,7 * 24 * 60 * 60 * 1000, pIntent);
} else { //repeat is off
manager.set(AlarmManager.RTC_WAKEUP,alarmTime, pIntent);
}
Toast.makeText(MainActivity.getContext(), "Alarm Set ", Toast.LENGTH_SHORT).show();
//enable automatically resetting alarms when device reboots
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
In order to check if alarm is set, I am using this code:
public static boolean ChkActiveAtrm(int pos){
boolean Rtn = false;
int AlrmID[]=ListViewAdapter.GetAlrmSelectID(MainActivity.AlrmIDStr.get(pos),pos);
for (int i=0;i<AlrmID.length;i++){
boolean alarmUp = (PendingIntent.getBroadcast(MainActivity.getContext(), AlrmID[i],
new Intent(MainActivity.getContext(),AlarmReceiver.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp) {Rtn=true;}
} //end for
return Rtn; }
Does anybody else found this phenomenon ? Who do I get immediate indication for alarm set/not set ? Thank you
Upvotes: 1
Views: 682
Reputation: 29436
First thing, stop relying on AlarmManager
alone to save and manage alarms. There are many scenarios where alarms will be cleared or delayed etc.
Have a SQLite table etc. to properly track alarms, along with created,scheduled and triggered time stamps.
______________________________________________________________________
| id | name | repeating | time | interval | set_at | triggered_at |
______________________________________________________________________
| 0 | abc | 1 | 2394000 | 36000 | 2003900 | 1800094 |
______________________________________________________________________
Then:
Implement a boot intent receiver to read this table and schedule alarms for each record.
When an alarm setting is updated, update it in database and re-schedule that alarm.
when an alarm is triggered, update time stamps in corresponding database record.
Upvotes: 1