How android calender setInexactRepeating works? - android

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.

Related

How to create an alarm which will be called after several months?

I've just learned about the AlarmManager and tried to play around with it. As I understood the alarms are set by saying that it needs to be called after X miliseconds like in the code below:
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (sec * 1000), pendingIntent);
However, I'm concerned about the performance of system when it comes to long periods of time.
If I need to set alarm that will activate notification say after 10 months, what should I do? Do I need to convert needed period of time into miliseconds and pass it in the same way? Or there are other more efficient ways to work with long periods of time?
Yes, you have to pass it as milliseconds. I'm not sure why you're concerned about performance from that, there's no loss in performance form passing a large value instead of a small one. Your only real problem is that doing in X months, the length of a month isn't regular. I'd create a Calendar object for the end time and convert that to milliseconds to get it right.
Use like this
int month = 2;
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 30*month, pendingIntent);
How to set alarm for long duration has already been discusses above.
You just need to keep a check that if device gets restarted you reset your alarm because alarms gets canceled once system goes off.
link here

How to add time to another time - Android

I have timer for a task. And all the sessions will be added up to each other.
Let's say today user spent 5 minutes
Other day he spent 1 hour, here this one hour will be added to the 5 minutes
and so on..
So it will be the total time in one value..
How can I do this ? Is it by Milliseconds or Date object ?
Date is more useful when you are dealing with actual calendar dates.
If you just want to keep track of time intervals/durations, just have a long variable and keep on adding the durations to this.
EDIT : Long.MAX_VALUE is 9,223,372,036,854,775,807. So, you don't really need to worry about the overflow either.
You can keep track of all the milliseconds using Date().getTime(), which returns the time since Epoch. So whenever you need to add/remove, just take your Date object, invoke .getTime(), and add/remove from the total. Then when you're done, you can convert the milliseconds to whatever format you need.

Calculate start time for the repeated AlarmManager

I'm working on an Android application which uses AlarmManager as follows:
Long startTime = Calendar.getInstance().getTimeInMillis();
alarm.setRepeating(
AlarmManager.RTC_WAKEUP,
startTime,
15* 60 * 1000,
PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
I want to calculate the startTime. Let's say I need to run some code repeatedly after 15 minutes, but this after 15 minutes should not start from now (current time) but right from the next slot. Whenever I enable the alarm it should start it from the next available slot (which means I need to calculate the start time).
Example 1: If current time is 10:8, then the first run should be at 10:15.
Example 2: If it's 10:17, then first the run should be at 10:30.
Example 3: If it's 10:38, then the first run should be at 10:45.
As AlarmManager takes 2 time parameters in milliseconds, the first one when to start the alarm and the second as the repeat time in milliseconds (15 minutes).
If it's still unclear then I would say I need to run my code when minutes of the device are either one of the following values:
00, 15, 30, 45 irrespective of the hour value of the device.
First, bear in mind that Doze mode and app standby on Android 6.0+ will mean that you will not always get control when you want.
That being said, you need to adjust startTime. Call get(Calendar.MINUTE) on it to get the current minutes. Determine how many minutes you need to add to get to the next quarter-hour. Then, call add(Calendar.MINUTE, ...), where ... is the amount you need to add to get the minute value to the next quarter-hour. Using add() will handle incrementing the hour, day, etc. as needed.

Repeat scheduled tasks on selected days in Android sdk alarm manager

I have tried to do this without bothering the experts and have read numerous threads here and on other sites. It is clearly my brain not understanding what needs to be done in order for this to work.
My goal is that the app allows the user to enter a time and one or more days in a week. All of the GUI side and storing of the dates and times I have done, however to get the alarm manager to repeat, lets say every Monday at 14:00 and then can send at 14:02 . I have used the java Calendar object to hold the times and days of the week or even used date and day of the week of the month. These are then , as needed, converted to milliseconds for it to be read in by the alarm manager.
I then have used either the alarm manager set or set repeat methods to repeat the event. All I am able to do is get it to occur once and then if I change the emulator date and time to another Monday nothing happens.
The GUI holds the hours and minutes in required variables and then these are used against the calendar objects.
The alarm manager calls a broadcast receiver for the event to occur.
Please can someone simply give an example on how to set specific days such as Monday , Wednesday Friday. I know that separate alarm managers are needed for each day and at the moment I have just focused on Monday as my main test.
Links viewed:
How can i Repeat the Alarm in android for only Monday, Tuesday and Friday
How to repeat the alarm for "n" days at a particular time
how to repeat alarm after 1 day in android
Managed to figure this out now and so follows my answer:
The following code calculates the remaining days between now and the day needed for the scheduled task. the variable whichday is passed via parameter from the method this code belongs to. In the understanding of this whichday represents days of the week 1 through to 7 where 1 is Sunday , 2 is Monday and so .
//This gets the current day of the week as of TODAY / NOW
int checkcurrentday = getcurtime.get(Calendar.DAY_OF_WEEK);
// This calculates the days between now and the day needed which is represented by whichday.
int numberofdays = Calendar.SATURDAY + whichday - checkcurrentday;
//Now add NOT set the difference of the days to your Calendar object
tMondayOn.add(Calendar.DATE, numberofdays);
Well, you need to first use the Java Calendar API (or Joda!) to figure out when the next monday is. Set the alarm to to that time in milliseconds then use setRepeating and pass in a long that represents the interval of one week.

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