I am using Alarm manager to run alarm at specific time every day. Below is the code
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, OnAlarmReceive.class);
PendingIntent pendingIntent =PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*60*60*1000, pendingIntent);
I am Setting alarm at 12AM every day. And Below is the code for BroadCastReciever
#Override
public void onReceive(Context context, Intent intent)
{
System.out.println("Time is 12 Am");
Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
}
Problem in this code is Alarm is Triggered As soon as i Run the Application Irrespective of time. Any help will be Appreciated. Thank You
The alarm will only fire immediately if you set the alarm in the past.
E.g. it is now 10:00h and you want to set an alarm every day at 09:00. To avoid this, you have to look what time it is now, and shift the alarm 1 day if that is the case... This allows you to use the setRepeating method (which is more precise than setInexactRepeating)
This fixes the issue:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
if(Calendar.getInstance().after(calendar)){
// Move to tomorrow
calendar.add(Calendar.DATE, 1);
}
//... continue like before by setting the alarm
I had the same issue as you and I couldn't get it to work. Plus all the examples I could find were also setting a specific date, not only just a time. This should work for you:
Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent); //Repeat every 24 hours
Hope this helps you fix your problem!
try to use this:
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, intent);
You could use something like this:
Calendar cal = Calendar.getInstance();
if(cal.getTimeInMillis() < System.currentTimeMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 7);
}
Related
I have an alarmManager that repeats at 9AM every day that calls a service. I would like to trigger the alarm every day (repeating) at 9AM with Service A, Noon with Service B, and 4PM with Service C.
My current method of doing this it to repeat every 3 hours and get the current time in the Service and figure out which action should be triggered based on the time, but this feels overly hacky. Here is my code. I wish I could instantiate multiple AlarmManager instances but I doubt I can given the way it is initialized.
Intent i_notifcreate = new Intent(this, NotifCreator.class);
PendingIntent pi_notifcreator = PendingIntent.getService(this, 0, i_notifcreate, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 00);
Log.e("NextAlarm", calendar.getTime().toString());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, pi_notifcreator);
pseduocode inside Service
if(time == 9AM){
A()
} else if (time == noon){
B()
} ... etc
I was able to figure it out using this poorly titled question alarmmanager 2 times
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 05);
cal1.set(Calendar.MINUTE, 45);
cal1.set(Calendar.SECOND, 00);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 17);
cal2.set(Calendar.MINUTE, 30);
cal2.set(Calendar.SECOND, 00);
// Test if the times are in the past, if they are add one day
Calendar now = Calendar.getInstance();
if(now.after(cal1))
cal1.add(Calendar.HOUR_OF_DAY, 24);
if(now.after(cal2))
cal2.add(Calendar.HOUR_OF_DAY, 24);
// Create two different PendingIntents, they MUST have different requestCodes
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent morningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
PendingIntent eveningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);
// Start both alarms, set to repeat once every day
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);
Up until now, every day at 11 o'clock an alarm manager triggers a procedure like so:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long alarmTime = calendar.getTimeInMillis();
alarmTime = alarmTime + (AlarmManager.INTERVAL_DAY);
Intent intent = new Intent(context, SyncReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1300, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY, pendingIntent);
Now I gotta change this procedure to go off at a random hour each day, how can I do that with the least amount of change?
Replace setRepeating() with set(). In SyncReceiver, choose the next random hour and set() a fresh alarm to trigger SyncReceiver at that time.
The below code triggers alarm randomly at any time, but I want to trigger it only at 8 AM and daily.
What I am missing, please help. Thanks in advance.
Intent alarmIntent = new Intent(context, NotifyingDailyService.class);
PendingIntent alarmPIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
// Set the alarm to start at approximately 8:00 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
AlarmManager alarmMgr= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(alarmPIntent);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmPIntent);
I believe AlarmManager.INTERVAL_DAY is only appropriate for calls to setInexactRepeating(). I think you need to change AlarmManager.INTERVAL_DAY to the number of milliseconds in a day.
INTERVAL_DAY DOCUMENTATION
http://developer.android.com/reference/android/app/AlarmManager.html#INTERVAL_DAY
ALARM MANAGER EXAMPLE
Android: How to repeat a service with AlarmManager every 15 minutes, but only run from 8:00AM to 18:00PM?
I solved the question myself by using broadcast rather than service.
Intent alarmIntent = new Intent(context, DailyNotificationAlarmReceiver.class);
PendingIntent alarmPIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
// Set the alarm to start at approximately 8:00 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
//System.out.println("Alarm set at 8 clock");
Toast.makeText(context, "Alarm set at 8 clock", Toast.LENGTH_LONG).show();
AlarmManager alarmMgr= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.cancel(alarmPIntent);
//alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmPIntent);
//alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),calendar.getTimeInMillis()+ DateUtils.DAY_IN_MILLIS, alarmPIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, alarmPIntent);
I am trying to run service on every day only on Specific time,
lets say every day 8 :00 am
and here I found two methods as
public static void setScheduleMessage(Context context, int hours,
int minuts, int seconds) {
Calendar calendar = Calendar.getInstance();
// 8 AM
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minuts);
calendar.set(Calendar.SECOND, seconds);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
new Intent(context, MessageSchduledService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
But problem with this method is, it is running every hour after specified time.
and here is second method I used which is not executing my service ?
public static void setScheduleMessage2(Context context, int hours,
int minuts, int seconds) {
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());// set the current
// time and date for
// this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, hours);
cal.set(Calendar.MINUTE, minuts);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(context, MessageSchduledService.class);
PendingIntent pintent = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pintent);
}
POST :
In my app I just want to push message from my app Database to Message Data base at specific time every day.
please if anybody having an idea about this, then let me know what i am doing wrong in these methods ?
Have a look Alarms
# RTC examples
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
or
In your code
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pintent);
replace
AlarmManager.INTERVAL_DAY
to
24 * 60 * 60 * 1000
. which means repeat alarm exactly after 24 hours
I am using alarm manager to assign the alarm at specific time. I encountered a problem which is if I set the time earlier than the current system time, then the alarm action will be fired instantly. How to prevent this behavior ? Thanks
My code:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context,AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 234324243, serviceIntent, 0);
/*sample of setting eariler time
timeAlarm.set(Calendar.YEAR, 2013);
timeAlarm.set(Calendar.MONTH, 10);
timeAlarm.set(Calendar.DAY_OF_MONTH, 13);
timeAlarm.set(Calendar.HOUR_OF_DAY, 12);
timeAlarm.set(Calendar.MINUTE, 9);
timeAlarm.set(Calendar.SECOND, 0);
*/
alarmManager.set(AlarmManager.RTC_WAKEUP, timeAlarm.getTimeInMillis(), pendingIntent);
This is because the android alarm manager is designed by this.
You can modify your time if the time earlier than the current system time by 1 day or more.
like this:
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
if (c.getTimeInMillis() < System.currentTimeMillis()){
c.add(Calendar.DAY_OF_MONTH, 1);
}