Getting Milliseconds from Calendar for a Month - android

How can I get Time in Millisecond from Calendar Object in order to set an interval in Alarm Manager I am using the following code:
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, sender);
and my calculation for interval for getting One month of MilliSeconds is as follows:
Calendar monthCal = Calendar.getInstance();
monthCal.clear();
monthCal.setTimeInMillis(System.currentTimeMillis());
monthCal.add(Calendar.MONTH, 1);
interval = monthCal.getTimeInMillis();
but I am not getting any repeating Alarms.

You can do the following:
Calendar c = Calendar.getInstance();
c.set(year,month,1); // Set the year and month you want milliseconds for
long daysInMonth = c.getActualMaximum(Calendar.DATE); //Get how many days this month have
long millisInMonth = daysInMonth*dayMillis; //Multiply the amount of days with how many milliseconds a day consists of (86400000L)

Related

Setting alarm android

I have a string that may contain value 07:00 or 11:00 0r 15:00
I want to set alarm for that time for today. it doesn't seem to work
One option is to create a calendar object set to midnight, and then offset it by the hours and minutes you have:
String alarm = "13:00";
Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long millis = c.getTime().getTime();
millis += 1000*60*60*Integer.parseInt(alarm.substring(0, 2));
millis += 1000*60*Integer.parseInt(alarm.substring(3, 5));
Demo
An alternative to this would be to use two SimpleDateFormat masks to parse out the timestamp you want.

Calculating the trigger time for an alarm when today set time passed in android?

In my Alarm application i have the following function for calculating the trigger time for the alarm , and its working perfect if the alarm time > current time , but if the alarm time is passed " Today Set time passed" current time > alarm time the alarm should fires tomorrow but in my function it fires in the same day after a few hours or minutes based on the difference amount . How can i fix this problem? what is the condition i should add to my function to wait 24 hours to tomorrow if the alarm set time is passed ? please help me
private long getTriggerTime(int hour,int minute)
{
long triggerTime = System.currentTimeMillis() ;
Calendar calendarNow = Calendar.getInstance();
int currentHour = calendarNow.get(Calendar.HOUR_OF_DAY);
int currentMinute = calendarNow.get(Calendar.MINUTE);
int HourDifference = currentHour-hour;
int MinuteDifference = currentMinute -minute;
if(HourDifference < 0 ){ HourDifference = HourDifference*(-1); }
if(MinuteDifference < 0){ MinuteDifference = MinuteDifference*(-1); }
triggerTime = triggerTime + ( HourDifference*60*60*1000) + (MinuteDifference*60*1000 );
return triggerTime;
}
Here how i use this function
am.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerTime(hour,min),AlarmManager.INTERVAL_DAY ,sender);
Note : hour , min in 24hour format
here is the sample code for alarm to fire if the alarm time > current time ,else it will for 24 hours.....
private long getAlarmTimeInMillis(int hour,int minute){
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);//
calendar.set(Calendar.SECOND, 0);
if (calendar.getTimeInMillis() < System
.currentTimeMillis()) {
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+ 1);
// add 24 hours to the calender.getTimeMillis
}
return calendar.getTimeInMillis();
}
try this.....
Use this:
Calendar calendarNow = Calendar.getInstance();
calendarNow.set(calendarNow.get(Calendar.YEAR), calendarNow.get(Calendar.MONTH),
calendarNow.get(Calendar.DAY_OF_MONTH),
calendarNow.get(Calendar.HOUR_OF_DAY),
calendarNow.get(Calendar.MINUTE), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendarNow.getTimeInMillis(),
24 * 60 * 60 * 1000, pendingIntent);

Scheduling a task for certain days of the week in Android

As the title implies, I'm looking to schedule a task to run on certain days at certain times. For example, I might have it run at 5:00 every Tuesday and Thursday. I've seen several scheduling methods for Android, but all of them seem to operate in the form of "do task after n delay" or "do task every n seconds".
Now I could probably jury-rig it by having it calculate the time to the next execution during the execution of the task itself, but that seems inelegant. Is there some better way to do this?
You've to set an Alarm to perform those tasks. Most probably you will end up calling a Service once the alarm is triggered:
private void setAlarmToCheckUpdates() {
Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY)<22){
calendar.set(Calendar.HOUR_OF_DAY, 22);
} else {
calendar.add(Calendar.DAY_OF_YEAR, 1);//tomorrow
calendar.set(Calendar.HOUR_OF_DAY, 22); //22.00
}
Intent myIntent = new Intent(this.getApplicationContext(), ReceiverCheckUpdates.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)this.getApplicationContext().getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
However, if you need to set specifically a day:
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (weekday!=Calendar.THURSDAY){//if we're not in thursday
//we calculate how many days till thursday
//days = The limit of the week (its saturday) minus the actual day of the week, plus how many days till desired day (5: sunday, mon, tue, wed, thur). Modulus of it.
int days = (Calendar.SATURDAY - weekday + 5) % 7;
calendar.add(Calendar.DAY_OF_YEAR, days);
}
//now we just set hour to 22.00 and done.
Above code is a little bit tricky and mathematic. If you wan't something stupid aswell as easy:
//dayOfWeekToSet is a constant from the Calendar class
//c is the calendar instance
public static void SetToNextDayOfWeek(int dayOfWeekToSet, Calendar c){
int currentDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
//add 1 day to the current day until we get to the day we want
while(currentDayOfWeek != dayOfWeekToSet){
c.add(Calendar.DAY_OF_WEEK, 1);
currentDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
}
}

I cant get to set desire time of AlarmCLock in Android

Hi guys this is what i have so far:
PendingIntent sender = PendingIntent.getBroadcast(mainactivity, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
showmsg();
the alarm comes after 10 seconds How can I make it in desire time i want, I have set a timepicker so how can i do it depending on the timepickers time to ring the alarm?
Thanx.
With your TimePicker, call picker.getCurrentHour() and picker.getCurrentMinute(). Use these values to calculate the total milliseconds of the given time of day (12:00 pm would equal 43200000 ms, for example) by multiplying by the milliseconds in an hour (3600000) or minute (60000). Then get the milliseconds of today's date when it started at 0:00. That would all look like this:
//time of day in ms
long totalTimePickerMs = (picker.getCurrentHour() * 3600000) + (picker.getCurrentMinute() * 60000);
//today's date in ms
Calendar c = Calendar.getInstance();
Date d = c.getTime();
long today = d.getDay() + d.getMonth() + d.getYear();
long total = today + totalTimePickerMs;
Essentially you are getting today at midnight (0:00) in milliseconds and adding to it the milliseconds of the specific time of day.
Then like you did before, except set the alarm with total as the second parameter.

Calculating time for Android alarm

In Android we set an alarm by setting the time until it goes off in milliseconds. Is there an easy way to find how many milliseconds there are until a certain time (hh:mm) or do I just have to calculate it mathematically?
Thanks!
Save your current time in milliseconds as
Calandar calendar = Calendar.getInstance();
Long currenttime = calendar.getTimeInMillis();
Long settime= <your set time in milliseconds>;
Here you can calculate the difference as follows:
Long differencetime = settime - currenttime;
int dif=(int)differencetime/1000;
Here you can set the time in calendar:
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + dif);
Here you can set the alarm for the settime.
AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi1);
Check out the first argument for AlarmManager.set(): With RTC/RTC_WAKEUP, you can specify a fixed time rather than an elapsed time.
That said, if you need to use the elapsed time, it's pretty trivial to calculate the number of milliseconds that need to elapse. Worst case, you could use the Calendar and/or Date classes.
Date now = new Date(), b = new Date(year, month, day, hour, min);
b.getTime() - a.getTime();
And here's another way to get the time in milliseconds to a certain time:
Calendar c = Calendar.getInstance();
c.set(c.YEAR, c.MONTH, c.DAY_OF_MONTH, 17, 1, 0); // 5:01 pm
long alarmTime = c.getTimeInMillis();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 1000 * 60 * 60 * 24, pendingIntent); // Millisec * Second * Minute * Hour // Same time every day
RTC_WAKEUP allows the alarm to still activate when the phone is asleep. Use RTC if you want to wait until the user wakes up the phone themself.
Use am.set() if you don't want the alarm to repeat
Let me know if this helps.

Categories

Resources