I am having following code to set the alarm.It is working for single date.But in my application user can select multiple dates and and when he/she selects the multiple date i have to set the alarm on each selected date on selected time.eg- if user selects three dates 11,12,13 and selects time 8am then the alarm should ring on 11,12,13 on 8am following is my code where i am setting alarm for single date
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.DAY_OF_MONTH, 11);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Use different request code for each PendingIntent like this
final int requestId = (int) System.currentTimeMillis();
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, requestId, myIntent, 0);
Related
I am Creating Alarm Application with Setting Alarm of Multiple Days i.e Repeating Alarm.My Android Alarm Application View Like This,
i Have Done Code for this,
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(ALARM_ACTION_NAME);
alarmIntent.putExtra("AlarmID", m_alarmId);
PendingIntent alarmPI = PendingIntent.getBroadcast(this, m_alarmId, alarmIntent, 0);
//listofred is a ArrayList of int items.contains int valye for selected days...for My Example listofred:3,4,5,7
for (int i = 0; i < listOfred.size(); i++) {
// for alarm ...
calNow = Calendar.getInstance();
calSet = (Calendar) calNow.clone();
int day = calSet.get(Calendar.DAY_OF_WEEK); //current day...for example.13 dec 2014 - sat so, day = 7
calSet.set(Calendar.DAY_OF_WEEK, listOfred.get(i));
calSet.set(Calendar.HOUR_OF_DAY, time_picker.getCurrentHour());
calSet.set(Calendar.MINUTE, time_picker.getCurrentMinute());
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), (DateUtils.DAY_IN_MILLIS)*7,
alarmPI);
//parameter long intervalMillis.....(DateUtils.DAY_IN_MILLIS)*7 so that it will repeat after each 7 days...
}
My Problem is When i run this code it will set repeat alarm for only Saturday(i.e last object in listofred Arraylist) every time it set alarm for last object in Arraylist.
I know it is quite late to answer this, but isn't it because of the same pending intent being passed to each alarm event.
Perhaps this might help.
PendingIntent alarmPI = PendingIntent.getBroadcast(this, m_alarmId, alarmIntent, PendingIntent.FLAG_ONE_SHOT);
https://stackoverflow.com/a/3009690/1111127
I have seen a lot of tutorials for creating an AlarmManager and bringing up a notification at a specific date, but I was wondering how to do it for X days before.
For example, if I have an event on January 8th, 2014 and I want to set it a week before to Jan 1st, 2014, I would just set the alarm as this:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.DAY_OF_MONTH, Day-7);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
However what if the event was on January 4th, 2014, then I would want to set the date to December 28th, 2013. Is there a function that can do this? Or do I have to write a lot of cases?
the event was on January 4th, 2014:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 4);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long eventTime=calendar.getTimeInMillis();//Returns Time in milliseconds
long oneDay=AlarmManager.INTERVAL_DAY;//Converts 24 Hrs(1 Day) to milliseconds
int noOfDays=4
long reminderTime=eventTime-(noOfDays*oneDay);//Time in milliseconds when the alarm will shoot up & you do not need to concider month/year with this approach as time is already in milliseconds.
//Set alarm
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
Hope this helps!
get your current time in milliseconds and subtract the no. of days from it,here 1 day= 24*60*60*1000 = 86400000
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, (calendar.getTimeInMillis()-(no. of days * 86400000)), pendingIntent);
Why does the alarm set off immediately instead of the specified time?
Intent myIntent = new Intent(Notepad.this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(Notepad.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.YEAR, 2012);
calendar.set(Calendar.MONTH, 4);
calendar.set(Calendar.DAY_OF_MONTH, 5);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 4);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
If I use only calendar.add(Calendar.SECOND, 5);, it sets off after 5 seconds.
SOLUTION based on Nicholas' answer
Date dat = new Date();//initializes to now
Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_now.setTime(dat);
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY,Integer.valueOf(h));
cal_alarm.set(Calendar.MINUTE, Integer.valueOf(m));
cal_alarm.set(Calendar.SECOND,0);
if(cal_alarm.before(cal_now)){
cal_alarm.add(Calendar.DATE,1);
}
To me it seems that you are setting the calendar time and day to the alarm time. Maybe set up two different calendars and have one for the current time and another for the alarm time. Does that make sense?
Its about a small android application that will take hours as input from user for every day of week and turn on / off blue tooth device accordingly for the respective day regardless of the "Date". i.e the code should only check the day, hour and minute. i have used following code (for testing purpose for today i.e Friday) but it do not triggers the alarm.
//..........setting calender for MyAlarmService
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 5);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
// here it m setting the "Today"
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
when i remove the statement
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
The AlarmService triggers perfectly "Today" on the time specifed above i.e 11:05 AM
What am i doing wrong?
found a work around as follows.
//..........setting calender for Friday
Calendar calFri = new GregorianCalendar();
calFri.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
calFri.set(Calendar.HOUR_OF_DAY, friHrInt);
calFri.set(Calendar.MINUTE,friMinInt);
calFri.set(Calendar.SECOND, 0);
calFri.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
calFri.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
calFri.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
days = 13 - calFri.get(Calendar.DAY_OF_WEEK); // how many days until Sunday
calFri.add(Calendar.DATE, days);
//..........setting calender for Saturday
Calendar calSat = new GregorianCalendar();
calSat.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
calSat.set(Calendar.HOUR_OF_DAY, satHrInt);
calSat.set(Calendar.MINUTE,satMinInt);
calSat.set(Calendar.SECOND, 0);
calSat.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
calSat.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
calSat.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
days = 14 - calSat.get(Calendar.DAY_OF_WEEK); // how many days until Sunday
calSat.add(Calendar.DATE, days);
and then i use separater alarm service for these days as follows
//.........................The Following will make the alarm go off on friday......................................
Intent myIntentFri = new Intent(AndroidAlarmService.this, FriOffAlarmService.class);
pendingIntentFri = PendingIntent.getService(AndroidAlarmService.this, 0, myIntentFri, 0);
AlarmManager alarmManagerFri = (AlarmManager)getSystemService(ALARM_SERVICE);
//alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent2);
alarmManagerFri.setRepeating(AlarmManager.RTC_WAKEUP, calFri.getTimeInMillis(),interval, pendingIntentSun);
//.........................The Following will make the alarm go off on saturday......................................
Intent myIntentSat = new Intent(AndroidAlarmService.this, SatOffAlarmService.class);
pendingIntentSat = PendingIntent.getService(AndroidAlarmService.this, 0, myIntentSat, 0);
AlarmManager alarmManagerSat = (AlarmManager)getSystemService(ALARM_SERVICE);
//alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent2);
alarmManagerSat.setRepeating(AlarmManager.RTC_WAKEUP, calSat.getTimeInMillis(),interval, pendingIntentSat);
I am trying to set an alarm every 5th day of the week and the 24th hour of that day.
Here is the code i am using. Ive been reading over the Calendar and AlarmManager docs, a
and here is what i have came up with.
String alarm = Context.ALARM_SERVICE;
//Alert for game covers
am = (AlarmManager)context.getSystemService(alarm);
calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 5);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent Aintent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, Aintent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, pi);
Is this correct for what i want to do?
To get a Calendar instance, that points to a date 5 days in the future, you take the current date and add 5 days like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 5);
Then you set your alarm:
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);