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