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.
Related
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.
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 :)
I have an application where the user selects a date as well as a time (using DatePicker and TimePicker). Is there any way to combine all the values into one integer? Both the DatePicker and TimePicker return integers, if I add them up, will the value then be the selected date and time, or does it need to be done some other way?
The way I've been understanding date and time, is it gives the difference in milliseconds from a certain point. Based on that, I would guess that adding the amounts together would give the correct time (taking into account the different starting times of the various methods).
Thanks!
You can use a Calendar, set the components then get the date.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 37);
cal.set(Calendar.DAY_OF_MONTH, 13);
cal.set(Calendar.MONTH, Calendar.JANUARY);
Date d = cal.getTime();
long time = cal.getTimeInMillis();
I'm using Java's calendar to set an alarm at a specific date and time. I know how to make this work when the user selects a specific date and time. For example, if the user wants to set an alarm on July 17th, 2013 at 10:45AM, I'm using the following code:
//Get the calendar instance.
Calendar calendar = Calendar.getInstance();
//Set the time for the notification to occur.
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.DAY_OF_MONTH, 17);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
All of the above code works really well when I want to set an alarm at a specific date and time. My question is, how can I set a calendar instance where the user user wants an alarm to go off 20 minutes from the current date and time? So if the current time is 6:50PM, I need the alarm to go off at 7:10PM. How can I set this programmatically?
I tried to get the current date and time via the Java.util.calendar's built-in methods and tried adding 20 minutes to the Calendar.MINUTE variable. However, I don't think this will do the trick if the current time is less than 20mins away from midnight (the date will change), or 20mins away from another hour (the hour will change). How can I get around this problem? Thanks for all your help!
You want to look at calendar.add , it will increment the next field if you get overflow.
http://developer.android.com/reference/java/util/Calendar.html
You can also try this
Calendar c = Calendar.getInstance();
c.setTime(new Date()); //
c.add(Calendar.YEAR, 5); // Add 5 years to current year
c.add(Calendar.DATE, 5); // Add 5 days to current date
c.add(Calendar.MONTH, 5); // Add 5 months to current month
System.out.println(c.getTime());
You can try the calendar.set methods specified in this links.
How to set the calendar in android for particular hour
This worked for me , when I wanted to run the service on the specified time.
I have 3 different Date objects and I want to pass them to AlarmManager object to alert me when they occur.
May I need to use
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 2);
to add 2 minutes to the calender, but how to add a future date object to the calender ?
Do I need A Calender object, or I need a separate Calender object for each Date ?
Them for AralamManger do I need one object to 3 ?
I will advice you to use three different Calendar objects in which future dates will be set as below:
Calendar calendar1 = Calendar.getInstance();
Date future_date=new Date();
future_date.setHours(hour);
future_date.setMinutes(min);
future_date.set(year, month, day);
calendar1.setTime(future_date);
....OR.......
calendar1.set(year, month, day, hourOfDay, minute)
----------------
Look like u want to setup multiple alarm...below is link for answer
How can I setup multiple alarms in Android?