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);
Related
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);
i making a app where you can set alarms . it actually works but just one time.
and i need to do more than once .im trying to do an class schedule alarms.
for example i have class mondays at seven , so i need to start the alarm every monday. but also i have another class tuesday and have to do it the same.
here is my code that works pd-> rqs1 = 1
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, horai); // i put the hour with the interface
cal.set(Calendar.MINUTE,minutoi);///
cal.set(Calendar.DAY_OF_WEEK,dias.getSelectedItemPosition()+1);
cal.add(Calendar.SECOND, 2);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("name", curso); // i put the name of the curso
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(),
RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);`
so, i have been looking how to do it.
please help me . Thanks
1- Replace this
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);`
with this:
alarmManager.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);
2- Replace this
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(),
RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
with this:
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(),
RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT );
3- Add this inside onReceive:
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("name", curso); // i put the name of the curso
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(),
RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT );
AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);`
Try using FLAG_UPDATE_CURRENT instead FLAG_ONE_SHOT.
Also make sure to reschedule the alarms after device reboot. Here is a good example.
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.
I am trying to set periodically alarm every 30 minutes, but i dont think it is working. I use below code:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MyActivity.this, Alarm.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(MyActivity.this, 0, intent, 0);
long selectedTimeMiliseconds = (long) (TimeUnit.MINUTES.toMillis(30));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);
How to set the second parameter in method setInexactRepeating? Is it better to use setRepeating method?
Inexact is a save battery method, but it is... inexact.
AlarmManager have a problem, if the device is in sleep mode, the intent not start. You should use WakefulBroadCast.
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
Try this..
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);
I set alarm from one class using this code
Intent myIntent = new Intent(ClassOne.this, AlarmService.class);
pendingIntent = PendingIntent.getService(ClassOne.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentDate.getTime());
long when = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, (7 * 24 * 60 * 60) * 1000, pendingIntent);
Now I need to cancel the pending alarms from another class. Can I just use this code?
Intent myIntent = new Intent(ClassTwo.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(ClassTwo.this, 0, myIntent, 0);
AlarmManager alarmManagerCancel = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerCancel.cancel(pendingIntent);
Or there is a better/proper way to cancel pending alarms?
As long as the PendingIntent are equivalent as the one you used to register the alarm, it doesn't matter where you call cancel() from. Two PendingIntents are equivalent (equals() returns true) if the underlying intents and request codes are the equivalent.