In which condition set-repeat Alarm get canceled in android? - android

i Create application in android that schedule a alarm for every 20 minute. but they get canceled or not working after scheduled 5 or 8 times. is there any condition after that system cancel the scheduled alarm.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 20);
// Create a new PendingIntent and add it to the AlarmManager
Intent my123intent = new Intent(context, PolicyFormatDownloader.class);
my123intent.putExtra(commonGlobalVariables.IS_CALL_FROM_ALARM, true);
my123intent.putExtra(commonGlobalVariables.IS_CALL_MANUALLY, false);
PendingIntent pendingIntent = PendingIntent.getService(context, 12345,my123intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ (60 * 1000 * 20), 60 * 1000 * 20, pendingIntent);
please tell me the conditions in which alarm get cancel or clear by system.
thanks for ans in advance.

The alarm can be cleared in follwing two conditions:
On device reboot
All alarms set using an Alarm-Manager, are removed on device reboot
If you create a Pending Intent with same ID
On creation of similar PI with same ID-Value (as 12345 in this case), will overrride the previous PI.

Related

Set alarm for several days

I'm working on alarm clock app. I've faced with one problem. I do not know how to set alarm for several days. I've already tried the code that is below but in log I saw this Wed Apr 06(didn't change any date so it should be nearest tuesday and friday). What do I do wrong? May be I should set alarm separately for every other day?
This is my code:
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY, Calendar.FRIDAY);
//calendar.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
Log.e("Point_1","Calendar " + calendar.getTime());
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
Intent intent1 = new Intent(MyService_alarm.this,MyReceiver_Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService_alarm.this,intent.getIntExtra("Size", 1),intent1,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent);
Thank you.
That is because you are logging the time before setting the hour and minutes from the time picker,
your code si working fine but to display the time that was set to the Alarm in your log you have to move the Log.e to after you set the Calendar to the hour and minute from your picker so your code should look like this :
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY, Calendar.FRIDAY);
//calendar.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
Log.e("Point_1","Calendar " + calendar.getTime());
Intent intent1 = new Intent(MyService_alarm.this,MyReceiver_Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService_alarm.this,intent.getIntExtra("Size", 1),intent1,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent);
Also since you are making an Alarm for several days it would be wise to save all the set alarms and to add a receiver to detect when the device has been booted since your alarms are cancelled on reboot and will need to be added again.
The line
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY, Calendar.FRIDAY);
is not correct. If you look at the documentation you will see that there is no such method.
By writing Calendar.DAY_OF_WEEK you are telling system that you are entering some value as day of the week. But instead of one specific day you are entering two. Calendar object is used to store one specific date.
Therefore, in order to get set alarms for several days, you need to set each of the alarms separately. For this reason you may create separate Calendar objects or reuse one by changing the time. However, you have same receiver class for both alarms. Therefore, you need to create different pending intents to make alarm manager differentiate them. For this reason I have shown you the example with different request_code.
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar2.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
calendar1.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar1.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar2.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar2.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Intent intent1 = new Intent(MyService_alarm.this, MyReceiver_Alarm.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MyService_alarm.this, 1, intent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MyService_alarm.this, 2, intent1, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, calendar1.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent1);
alarmManager.setRepeating(AlarmManager.RTC, calendar2.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent2);

Android : AlarmManager triggers anytime

I have following piece of code which should be trigger alarm after 2 days. However It gets triggered after every 2 hours, some users says they get it after every 5 mins too.
settingDB.updateSetting("Notification",1);
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.HOUR_OF_DAY, 10);
Calendar_Object.set(Calendar.MINUTE,01);
Calendar_Object.set(Calendar.SECOND, 0);
//Calendar_Object.set(Calendar.DAY_OF_WEEK, 1);
// MyView is my current Activity, and AlarmReceiver is the
// BoradCastReceiver
Intent myIntent = new Intent(Setting.this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Setting.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//Log.w("alarm set for " , Calendar_Object.getTime().toString ());
/*
* The following sets the Alarm in the specific time by getting the long
* value of the alarm date time which is in calendar object by calling
* the getTimeInMillis(). Since Alarm supports only long value , we're
* using this method.
* 3*1000*24*3600
*/
//alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),3600*1000**24,pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), 86400000*2, pendingIntent);
Could you please help me to resolve the problem?
The calendar object passed to alarm manager is set to a fixed time. This will cause the alarm to go off when that time comes.
you could also do this
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
+ 60*1000,timeofAlarm pendingIntent);
The calendar object you passed seems to do the damage. Follow my example above , once the alarm will go off after like a min. Just to check if the alarm is working,(you may want to add anytime there ,its upto you).
Realtime is useful(and perhaps recommended) if you want your alarm to go off every given time, irrespective of user time state

Android: CountDownTimer vs AlarmManager

I have to run a piece of code every 5 minutes in my service. Will countdowntimer be killed since the app is not in foreground. If this is the case will alarmanager be better in running the code?
Thanks,
Sahil
I think especially if your code runs in a Service that an AlarmManager is the better alternative. You can use the AlarmManager to start your Service in a 5 minute interval like this:
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(context, YourService.class);
// Set action of Intent or add extras
long period = 5 * 60 * 1000; // 5 minutes
PendingIntent pendingIntent = PendingIntent.getService(context, alarmId, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), period, pendingIntent);
If you want to stop your alarm you have to use the same alarmId so it would be best if you put that id in a constant. Also note that you have to restart your alarm every time the phone reboots!

kill an alarm checker in android

I have an alarm checker in my activity (Groups.java) to start a service each few seconds:
public void lookForGroups()
{
int seconds = 40;
Intent myIntent = new Intent(Groups.this, GroupsTaskAlarmChecker.class);
pendingIntent = PendingIntent.getService(Groups.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent);
}
Also depending on sharedPreferences variable I start this service when I reboot device.
Is posible to "kill" or start that alarm checker depending of a value of a variable?
for example to automatically or manually sync my application.
Thank you very much in advance and sorry for mi english ;)
Not sure if I understand this...but you create an Alarm checker and want to kill it if it's already running?
alarmManager.cancel(pendingIntent)
should do it. According to the reference, it will cancel any alarms with a matching intent

Alarm manager triggered too many times

I have a problem regarding Alarm manager in Android.
I have the following code snippet to set an alarm that should be fired each week(once).
// Add the time and set when the notification will be triggered
Calendar setCalendar = item.getDate();
calendar.set(Calendar.MINUTE,setCalendar.get(Calendar.MINUTE)+10080);
//Create a new alarm intent
Intent alarmIntent = new Intent(ApplicationUtils.getApplicationContext(), AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(ApplicationUtils.getApplicationContext(), requestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.ELAPSED_REALTIME, sender);
And I have to following problem. When the week is changed, the notifications are coming up and they never stop.
Does anybody have any idea how can I set the calendar so the alarms are triggered once a week?
Thanks, Arkde
You're third parameters in setRepeating is incorrect. It should be an interval between the repeating alarms in milliseconds.
A week would be: 1000 * 60 * 60 * 24 * 7 .
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)
Check out the Code it help you.
Intent intent_for_every_second = new Intent(Activity.this, Notifier.class);
pendingIntent_for_every_second = PendingIntent.getBroadcast(Activity.this, 0, intent_for_every_second,0);
AlarmManager alarmManager_for_every_second = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager_for_every_second.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 1000,pendingIntent_for_every_second);

Categories

Resources