I'm using an alarm manager like this :
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
So my alarm runs at 8:30 every day, but let's suppose that the user's phone is off at 8:30, how can I make it so the alarm starts running whenever the user's phone is available and the hour is within an interval. Let's say the interval is [8h, 18h] so once the user's phone is on and the current time is within that interval the alarm should be triggered.
Also, how is it possible to stop the alarm at 18h (supposing that it started running before)
Related
I'm trying to schedule a repeating alarm clock for a specific time (repeating weekly). The closest thing I've got is:
Getting the exact time:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmHour);
calendar.set(Calendar.MINUTE, alarmMinute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, dateDelta);
long time = calendar.getTimeInMillis();
And then setting the alarm (where my question comes):
val clockInfo = AlarmManager.AlarmClockInfo(time, homePI)
alarmManager.setAlarmClock(clockInfo, alarmTriggerPI)
The problem is that to make it repeat, I'll need to manually trigger this code again.
If I use setRepeating instead of setAlarmClock then I lose the alarm notification (for newer phones).
Again the idea is just to start a normal alarm clock that if set to go off on a Friday, it will go off on every Friday.
Mike's comment is the right answer. In short, you have to manually trigger it.
I have an Alarm Manager which sends notification for every 24 hours. If i install app to phone before alarm time everything is okay. However if i install app to phone after alarm time during day, it sends notification immediately.
For example:
Alarm Time: 12.15
I installed app at time: 12.10 there is not any problem
Alarm time: 12.15
I installed app at time: 12.20 it send notification immediately however i want to send notification next day at 12.15:
I solved problem by getting the current time of system and comparing it with the alarm time. Is there another method of alarm manager class for this problem ?
here is my setting alarm code:
private void setAlarmTime()
{
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//bir gun
int interval = 1000 * 60 * 60 * 24;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 15);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
Log.v("alarm set", "alarm ayarlandi");
userScreenPreference();
}
I'm currently making a native Android app and I have code working to schedule a notification to appear on the device using the AlarmManager class:
Intent intent = new Intent(this, NotifyActivity.class);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, alarmIntent);
Two things:
The notification goes off every time the app is opened. I don't want this to happen.
The notifications is suppose to be going off at 9am device time (or so I'm led to believe). This is not happening and it looks like it goes off every 9 hours or so.
Can anyone tell me why the notification goes off every time the app is opened and why the notification is not only being triggered when the device reaches 9am?
Thanks
From the doc of AlarmManager:
If the stated trigger time is in the past, the alarm will be
triggered immediately
You are setting your alarm to 9:00 , if you launch after this hour, trigger time is in the past, so the alarm is going off when the app is opened.
You can check if the current time is after alarm, if so, you add one day to the alarm and it will going off the next day. For example:
Calendar cal = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
if (cal.before(cal_now)) {//if its in the past
cal.add(Calendar.DATE, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, alarmIntent);
Hope it helps,
This question already has answers here:
AlarmManager fires alarms at wrong time
(2 answers)
Closed 7 years ago.
I have four Repeating Alarms in my app each of which fires at different timings to do some important tasks.
They are
Alarm 1 - 12:30 AM
Alarm 2 - 01:00 AM
Alarm 3 - 06:00 AM
Alarm 4 - 12:15 PM
Every alarm fires no problems. Time interval for each alarms are 24 hrs. But each of the firing at random timings.
Alarm Set Time Actual Firing Time
Alarm 1 12:30 AM 06:31 AM
Alarm 2 01:00 AM 06:01 AM
Alarm 3 06:00 AM 06:01 AM
Alarm 4 12:15 PM Still not fired 4 hours after that time
My Code was
Alarm 1
Intent myIntent=new Intent(MainPage.this,db_restore.class);
PendingIntent pi=PendingIntent.getBroadcast(MainPage.this,1,myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000*60*60*24,pi);
Alarm 2
Intent mor_flag=new Intent(MainPage.this,MorningFlag.class);
PendingIntent mor_intent=PendingIntent.getBroadcast(MainPage.this,3,mor_flag,0);
AlarmManager mor_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
mor_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,mor_intent);
Alarm 3
Intent late_start1=new Intent(MainPage.this,late_start.class);
PendingIntent piLate=PendingIntent.getBroadcast(MainPage.this,2,late_start1,0);
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, piLate);
Toast.makeText(MainPage.this,"Alarm Service started for Late Start monitoring",Toast.LENGTH_LONG).show();
Alarm 4
Intent eve_flag=new Intent(MainPage.this,EveningFlag.class);
PendingIntent eve_intent=PendingIntent.getBroadcast(MainPage.this,4,eve_flag,0);
AlarmManager eve_alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.AM_PM, Calendar.PM);
eve_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60*24,eve_intent);
I don't know what I'm doing wrong. Please take a look and help me. Thanks.
Note: I'm checking this in API Level 19 Android 4.4.4 with minimum as 14.
From the API
DOCS(https://developer.android.com/training/scheduling/alarms.html)
As of Android 4.4 (API Level 19), all repeating alarms are inexact. This means Android synchronizes repeating alarms from multiple apps and fires them at the same time.
So if you want an exact alarm use Alarm Manager's
setExact (int type, long triggerAtMillis, PendingIntent operation)
method.
If you want repeating alarms with exact timing then you would have to write a logic where you will set an alarm (Not a repeating alarm) and when this alarm goes off set another alarm for next interval.
Try using AlarmManager.setExact() and give some rep to this post
For api levels below 19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time. Thou on api levels 19 and above this will no longer work. There was change in android so that all repeating alarms are inexact. So if you would like to achieve exact repeating alarm you should schedule alarm with AlarmManager.setExact() and then when alarm triggers do it again for next week and so on every week.
I'm new to android and using alarmManager and I was wondering if there is a way to set an alarm in android that triggers for example every monday until a certain specific date. Like this :
Start date 10/09/15
Remind me something every monday at 2:30 pm
Until
End date 11/09/15
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 30);
int weekInMillis = 7 * 24 * 60 * 60 * 1000;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
weekInMillis, PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT));
Above code snippet sets an alarm for 2:30 PM that repeats itself every week. Tweak calendar for varying the time at which the alarm goes off. For example, the coming Monday.
When the alarm goes off, it sends a broadcast which will be received by ReminderWakefulBroadcastReceiver, a custom receiver containing the code that you want to run every Monday at 2:30 PM. This code should also check whether it is time to cancel the alarm and if it is, the following code cancels it:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class));
References:
AlarmManager, Scheduling Repeating Alarms, PendingIntent
If you know how to setup an Alarm, the solution is quite simple:
1) At the time you setup the Alarm, calculate the maximum timestamp you want it to run, and save it as a local preference.
2) Then in the Alarm code itself, each time it is triggered you can make a first test to see if the current timestamp is before or after your limit preference saved at first time.
3) If reached, then cancel the Alarm as #karthik said. If not, keep your code going...