AlarmManager Android trigger alarm every day at 10 AM - android

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()

Related

android best way to set an alarm in android

i have a general question to set an alarm in android.
at the moment, the user can chose a date and time with a date picker (which is a date in the future).
then i will set the the delay time for the alarm.
I convert the chosen date & time in milliseconds and subtract System.currentTimeMillis() = this difference I set for the delay of my alarm.
my question is, if this the best way to calculate the delay or is there a better solution?
this calculation i use for update an alarm, too
Look at the Android Alarm Manager class. You can use it to set alarms at specific times, without calculating the delay.
final AlarmManager am = (AlarmManager) App.instance.getSystemService(Context.ALARM_SERVICE);
Calendar c = GregorianCalendar.getInstance();
c.set(2015, 12, 20, 10, 30);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), operation);
where operation is a PendingIntent of the action you want to do when the alarm triggers.
My solution for you here:
You should convert normal datetime to timestemp formart
You can calculate by subtract future timestemp (which is set by alarm time) with current timestemp of phone to get return value then convert it to hour, munite and day number.
If after calculate get result is zezo then you notify alarm to user.
else nothing to do

How to set repeating alarm on week days[Day and Night], Week end in simple manner?

I need to set on repeating alarm for my below cases,
Week Days[Day(8AM -8PM), Night(8PM -8AM)]
Week End
My Application mode changes during each period of time.
I have used the below code to set an alarm for particular day[Monday].
//Setting Alarm on Monday 8AM
Calendar alarmCalendar= Calendar.getInstance();
alarmCalendar.set(Calendar.DAY_OF_WEEK, 2);
alarmCalendar.set(Calendar.HOUR, 8);
alarmCalendar.set(Calendar.MINUTE, 0);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.AM_PM, am);
Above code will set the alarm for Monday. How to Set separate alarm for Week days for 8AM and 8PM. How to set Alarm for Week end also. Whether I have to replicate the same above code to set on alarm for each and every day.
Or else any option to set the alarm in simple manner?
Please help me on this.
You can setup the alarm to the Monday with a repetition of every 7 days, and make it for the rest of the work days by the same way.

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.

Setting android alarm manager on certain days

I'm building an app that uses alarm manager. The user sets day and time and there is 7 checkboxs, one for each day of the week and the ones they tick the alarm will go off on them days. Like i know you can put date and time into the alarm manager is there any way i can put like day and time into alarm manager and them it will go off on that day every week?
AlarmManager.setRepeating takes as parameter:
type One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP}, RTC or RTC_WAKEUP.
Here you will want RTC or RTC_WAKEUP
triggerAtTime Time the alarm should first go off, using the appropriate clock (depending on the alarm type).
Here you give the date/time of the first alarm (I believe this must be UTC time, so be careful)
Calendar calendar = new GregorianCalendar(2011, Calendar.APRIL, 19, 23, 12);
long firstTime = calendar.getTimeInMillis();
interval Interval between subsequent repeats of the alarm.
To repeat every week, you will give as interval the number of milliseconds in a whole week:
long interval = 1000 * 60 * 60 * 24 * 7;
or
long interval = 7 * AlarmManager.INTERVAL_DAY;

Android - Find out what hour of day it is

I want to use and alarmManager that sets a repeating alarm to go off on the hour, every hour. I know how to set a repeating alarm every hour but not how to actually set it from the top of the hour, I need to know this value for the 'whatTime' variable below.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, whatTime, 1*60*60*1000, operation);
Also I want to be able to set a flag that for e.g. - if the time happens to be between 4 and 8 in the daytime, perform some operations, otherwise don't bother.
So I really need to know how to find out the hour of the day, can anyone tell me how to do this? Many thanks
Try:
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
Calendar.HOUR_OF_DAY gives you the 24-hour time.
Calendar.HOUR gives you the 12-hour time.

Categories

Resources