AlarmManager setRepeating for Multiple Instances - android

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);

Related

How to trigger a background service in android on specific time?

I know how to trigger a background service in android even on specific time. I have used AlarmManager to achieve this, but I want to start that service daily on that time.
Suppose I want to start it on 12pm, but the service should keep going on after that. I have also used the stopitself() method, but that does not work.
Here is my code:
void alram(Context ctx){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.AM_PM,Calendar.AM);
Intent serviceIntent = new Intent(ctx, ServiceClass.class);
PendingIntent servicePendingIntent =
PendingIntent.getService(ctx,Service_Apps.SERVICE_ID,serviceIntent,0);
AlarmManager alarm = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),servicePendingIntent);
alarm.setRepeating(
AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
servicePendingIntent
);
}
Hi Please use this code it will help to fill your desires.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(this, 0,
new Intent(this, Service_Apps.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);

How to trigger the alarm multiple times on same day?

For example, first alarm to be triggered at 10:00 am, second alarm to be triggered at 11:15 am and Third to be at 11.45 am.... Like this without any common difference between each instance I need to trigger alarm multiple times.
If I had common difference than repeat function can be used, but without common difference between times I need to trigger it. So, how can i trigger the alarm multiple times?
You can set multiple alarm
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY, HOUR_1);
calendar1.set(Calendar.MINUTE, MINUTES);
calendar1.set(Calendar.SECOND, SEC);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.HOUR_OF_DAY, HOUR_2);
calendar2.set(Calendar.MINUTE, MINUTES);
calendar2.set(Calendar.SECOND, SEC);
Calendar calendar3 = Calendar.getInstance();
calendar3.set(Calendar.HOUR_OF_DAY, HOUR_3);
calendar3.set(Calendar.MINUTE, MINUTES);
calendar3.set(Calendar.SECOND, SEC);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), ALARM_INTERVAL , pendingIntent1);//here interval is 24*60*60*1000
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), ALARM_INTERVAL , pendingIntent2);//24*60*60*1000
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar3.getTimeInMillis(), ALARM_INTERVAL , pendingIntent3);//24*60*60*1000
}
from this link "http://www.satyakomatineni.com/item/3503" , ArrayList<Calendar>calenderList=new ArrayList<Calendar();
calender=Calendar.getInstance(TimeZone.getDefault(),Locale.getDefault());calender.set(Calendar.DATE,resultDay);
calender.set(Calendar.MONTH,8);
calender.set(Calendar.YEAR,2016);calender.set(Calendar.HOUR_OF_DAY,14);calender.set(Calendar.MINUTE,30);calender.set(Calendar.SECOND,10);
for(int n=0;n<calenderList.size();n++)
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Intent intent = new Intent(TvActivity.this,TVAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(),n, intent,PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setExact(AlarmManager.RTC_WAKEUP,calenderList.get(n).getTimeInMillis(), pendingIntent);

Android how to set AlarmManager alarm to go off at a random hour every day?

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.

Alarm gets trigger randomly but not daily

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);

Service run on specific Time Android

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

Categories

Resources