Android - Find out what hour of day it is - android

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.

Related

alarm fires off 8 hours different of my timezone

Alarms in my application fire off 8 hours after the time I set in Calendar.HOUR_OF_DAY. My time zone is UTC +8.
Could you please help me to make the alarms fire off exactly as the time I select using a TimePicker, and also to make it flexible for other time zones.
Thank you
Ev_calendar.set(Calendar.HOUR_OF_DAY,xxx3);
Ev_calendar.set(Calendar.MINUTE, xxx4);
Ev_calendar.set(Calendar.SECOND, 0);
long Evening_TimeInMillis=Ev_calendar.getTimeInMillis();
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
Evening_TimeInMillis,AlarmManager.INTERVAL_DAY,Evening_pi);

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.

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

How android calender setInexactRepeating works?

I am developing small android application. And I want to do something in my application after some minutes. These minutes are not static one these are dynamic ones.
So i am using android calender setInexactRepeating for this.
My code looks like this
Calendar cal = Calendar.getInstance();
// Start something after 4 minutes
cal.add(Calendar.MINUTE, 4);
get_alaram_service().setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 1000*300, get_pendingintent());
So this will work setInexactRepeating after 4 min it will run my pending intent and after that it will keep repeating this for this much amount of time. (1000*300).
So my problem is that in setInexactRepeating 2nd parameter is for at what time I want to start my timer and 3rd parameter for repeating this thing. Now 2nd parameter tales value in milisec. I tried to pass my own value of minutes in milisec like(1000*300) then its not working properly. I don't how its working properly. When I checked cal.getTimeInMillis() it is very big integer number. what is that actually.
Am i doing something wrong need your help thank you...
Although this isn't explicitly stated in the documentation the second parameter (triggerAtMillis) is time in milliseconds since the Epoch. This is what Calendar.getInstance() returns. Calling this method will return the current time. This is a big number, since it is actually the number of milliseconds after 1/1/1970. You then need to add something to it (e.g. 4 minutes) to define when the AlarmManager will first fire.

Categories

Resources