How to set an AlarmManager for preselected days? - android

I'm creating an alarm widget, and it works, however, I'm currently using it for a single day, I'd like to know how to define it for predefined days, let's say for example I want to setup the alarman for monday, wednesday and friday, how can I accomplish that?
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, dPicker.getYear());
cal.set(Calendar.MONTH, dPicker.getMonth());
cal.set(Calendar.HOUR_OF_DAY, tPicker.getCurrentHour());
cal.set(Calendar.MINUTE, tPicker.getCurrentMinute());
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), testThis(this));
I'm currently using a TimePicker and a DatePicker, but I will change the design to use checkboxes, each per day, but I'm not sure how to define the alarm for the selected days, any help? thank you so much.

Defining them at once i don't know if this can be achieved, but i have another scenario to use.
Say that you adjusted the Alarm to fire on Monday, Tuesday , Thursday. you will adjust the AlarmManager just for Monday and on Monday it should fire and checks for the next alarm ( which will be on Tuesday ) and so on. Save all you alarms in a database and take them out one be one and in each alarm check for the next one ( if exists )

You can use the Calendar.DAY_OF_WEEK api like this:
cal1.set(Calendar.DAY_OF_WEEK,2); //monday
cal2.set(Calendar.DAY_OF_WEEK,4); //wednesday
cal3.set(Calendar.DAY_OF_WEEK,6); //friday
and sets 3 alarmMgr like this:
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, testThis(this)); // every monday
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, testThis(this)); // every wednesday
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, testThis(this)); // every friday

Related

Set Weekly alarm After a particular date

I am developing one android application. I need to set a weekly alarm.
This is how I am setting weekly alarm:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, WEEK_DAY_NUM);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, sec);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000, broadcast);
It is working as expected.
But I want to set alarms as below:
A user can select the date, day and time
If user Selected Date is Already Passed (i.e previous date compare to today's date)then I am setting alarm using today's date
If user selected date is a future date
consider this case:
Today date is 21 Aug 2018 and user selected date is 23 Aug 2018
and user selected day is Friday and Time is 4 PM
How should I set alarm for this case? so it will start weekly repeating alarm every Friday at 4 PM after 21 Aug 2018.

Android Alarm Manager Set Repeating every six month at Specific Date [duplicate]

Hi I am trying to make reminders above api 11.
I did all things with and working on time also means
if i set time , its working but if i want to set reminder for tomorrow or any day
it is not able to do..
so i put this calender code to set alarm which is working.
With time working
Calendar calendar = Calendar.getInstance();
// working time only
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 00);
AlarmBcastReceiver alarmBcastReceiver = new AlarmBcastReceiver();
alarmBcastReceiver.SetAlarm(this, calendar);
Now if tried to put date in this calender.
this is not working i.e.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
// working time only
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 00);
AlarmBcastReceiver alarmBcastReceiver = new AlarmBcastReceiver();
alarmBcastReceiver.SetAlarm(this, calendar, notiId);
I also tried to set this
//calendar.set(year, month, day, hour, min);
but it also not working.
You are passing wrong moth. You are passing 10 and want to test for October. You will need to pass it considering 0-11 for January - December.
Which means,
change your month to 9.
calendar.set(Calendar.MONTH, 9);
That will make the alarm date of October. So make changes and test.
A reference
Hope this helps.

Repeating alarm for one /two weeks?

I am developing an android application that trigger alarm for one/ two weeks or for long life. Also, this alarm will be repeated per day according to the number of pills. For example,
if the user has to take the pill for only two weeks and on every day he needs to take three pills. So, the alarm will be repeated for only Two weeks. and on every day will be repeated after 8 hours, suppose he takes the pill on the noon 12pm then the second pill should be at 8pm,4am and so on. (I want the alarm to trigger on these period of time)
This is my code, I used Spinner as dropdown list to select the duration time for the pill
if(deuTime.equals("Two week")) {
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 14);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
long alarm_time = calendar.getTimeInMillis();
if (calendar.before(Calendar.getInstance()))
alarm_time += AlarmManager.INTERVAL_DAY * 7;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm_time,
1000*60*60*8, alarmIntent);
}
thank you for your help :)

AlarmManager Android trigger alarm every day at 10 AM

I would like to run automatically, each day at predefined time (for. example at 10 AM) some code in my app.
I trying to do using the:
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 120 , pi);
But setRepeating accepts only time in miliseconds.
I would like to ask, is possible to set something like 10:00, REPEAT_DAILY ?
Thanks for any advice.
What about
am.setRepeating(AlarmManager.RTC_WAKEUP, tenOclockToday,
AlarmManager.INTERVAL_DAY, pi);
You'll need to set long tenOclockToday equal to the millisecond value of 10:00 today (unless it is past 10:00, then you can set it to 10:00 tomorrow). You'll need to use a Calendar instance and set the time to 10:00, then get the milliseconds from the Calendar using calendar.getTimeInMillis()

set the time in Alarm manager Android - alarm fired instantly

Here i am trying to set the alarm by using AlarmManger class. It is working fine with me but when i set the alarm time after Hours or mins from time picker,It will start the instantly when i save that alarm. the alarm. I need to alarm go off until i set the time.
Below is my code is working but starts the alarm immediately when i save.
I am setting time only with the time picker.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH,mHour,mMinute);
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
If i take below code alarm is not working..
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, mHour);
calendar.set(Calendar.MINUTE, mMinute);
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
Help me should i change in the to work properly.Thanks in advance...
The second code should normally work for dated alarms.
Keep in mind:
If you set the HOUR and MINUTE you set them for the current day eg HOUR = 1 and MINUTE = 30 means you set an alarm for 01:30 AM.
If it is over, you might get the alarm right now.
When you like to create an alarm in the future with 1:30 to go, then use the calendar.add(..,..) method.
I'm not sure what you're trying to do with this line:
calendar.set(Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH,mHour,mMinute);
but I'm pretty certain it doesn't do what you want. This will set the calendar to be the hour & minute you have chosen on the 5th of March of the year 1 AD. When you convert this to milliseconds, you'll get 0 out, because that's before the earliest date that can be represented in milliseconds.
You also have a different Calendar API related problem in this line:
calendar.set(Calendar.HOUR, mHour);
Here, your problem is that the Calendar.HOUR field refers to the hour in 12-hour notation, whereas your mHour is presumably using 24-hour notation (otherwise you'd also need an AM/PM field to hold a full day's worth of times). You want Calendar.HOUR_OF_DAY instead.

Categories

Resources