Alarm manager triggers just after alarm set - android

I am working on app in which I want to set alarm on weekly basis, e.g. on every sunday 10:00pm or every sunday and monday on 10:00pm. but whenever I start alarm manager it triggers immediately, I know that I am creating an alarm for the day which has passed but I still want to add alarm for next coming sunday, and I am unable to achieve that. here is a code what I have done so far.
Intent intent = new Intent(getBaseContext(), SchedulerReciever.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getBaseContext(), i, intent, 0);
intentArray.add(pendingIntent1);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 1);
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.PM);
Long alarmTime = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY*7,alarmTime, pendingIntent1);

Related

AlarmManager setRepeating for Multiple Instances

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

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.

Set Repeating Alarm Every Day at Specific time In Android

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

Don't start the notification When the activity already opened

I set an AlarmManager to open notification every 24 hours.
But every time I open the activity, the notification starts.
So how to set it not to show the notification when the activity is already opened?
This is my AlarmManager code:
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Intent intent = new Intent(this, myService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
long when = calendar.getTimeInMillis(); // notification time
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC, when, 24*60*60*1000, pendingIntent); //every 24 hours
Thanks ,
Your when variable points to the current time, hence the alarm will be fired right at execution of that part. Change when to calendar.getTimeInMillis(). Your calendar variable is set to the hour 20 of the day and repeating the day after. Here is a working copy

Alarm fires if the time is eariler than the current system time in android

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

Categories

Resources