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
Related
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.
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.
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.
Is there any additional information available from Intent.ACTION_TIME_CHANGED? There's nothing in getData() or getExtras().
I would like to know:
The time phone had before setting new time;
Who has changed the time: the user (manually) or the phone carrier?
Any other way to get those informations are welcome!
I looked into source code of Android and this broadcast doesn't have any extras. So, there is no way to learn this info.
You can do one thing if the accuracy of previous time is not that important. You can get the previous time with +/- 1 minute accurate by following way..
Register for broadcast action ACTION_TIME_TICK (This will be broadcasted every minute).
When ever the time ticks, if there is a difference of more than 1 minute between your current time and last tick time, you can infer that there occured a time change. After that you just update the new time to shared preference. Thats all.
Register for ACTION_TIME_TICK broadcast.
When broadcast received :
2.1 If first time broadcast, Enter the current system time to Shared Preference.
2.2 else compare the current time with previously entered time and if occurs a difference of more than 1 minute, means the time time has changed. Then update the new system time to SP.
Happy coding.
I don't think getting the why the time changed is possible, though finding out the amount the time was changed should be possible by comparing System.currentTimeMillis() to the SystemClock.elapsedRealtime(), since SystemClock.elapsedRealtime() does not get adjusted in this case.
An example would be something like:
private long realtimeOffset = System.currentTimeMillis() - SystemClock.elapsedRealtime();
private void onReceive(Context context, Intent intent) {
if(Intent.ACTION_TIME_CHANGED.equals(intent.getAction()) {
long prevRealtimeOffset = realtimeOffset;
realtimeOffset = System.currentTimeMillis() - SystemClock.elapsedRealtime();
Log.i(TAG, "Clock was adjusted by: " + (realtimeOffset - prevRealtimeOffset) + " ms");
}
}
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.