AlarmManager alarm not starting exactly at the change of minute - android

I am doing an alarm clock and although using exact alarms it is not starting at the change of minute:
For example I set is at 7:30 it will start somewhere between 7:30:00 - 7:31 (have not been able to check exact time).
I call the alarm like this:
// Create the alarm
CustomAlarmManager alarmManager = new CustomAlarmManager(getActivity());
alarmManager.scheduleRepeatingAlarm(getActivity(), alarmID, alarmHour, alarmMinute);
Which runs this function to create the alarm:
public void scheduleRepeatingAlarm(Context context, int alarmID, int alarmHour, int alarmMinute) {
// Create an intent to go to the notifications reciever class
Intent intent = new Intent(context, AlarmNotificationReciever.class);
// Bundle information and add to the intent for use later
Bundle extras = new Bundle();
extras.putInt("AlarmId", alarmID);
extras.putInt("AlarmHour", alarmHour);
extras.putInt("AlarmMinute", alarmMinute);
intent.putExtras(extras);
// Create a pending intent for the alarm id and intent
PendingIntent pIntent = PendingIntent.getBroadcast(context,
alarmID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Create a calender variable with the time for the alarm
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, alarmHour);
calender.set(Calendar.MINUTE, alarmMinute);
// Create a variable for the current alarm time
LocalTime currentAlarmTime = LocalTime.parse(alarmHour+":"+alarmMinute+":"+"00");
// Create a variable for the local time
LocalTime localDeviceTime = LocalTime.now();
// If the alarm has already elapsed today add one day to it so it runs for tomorrow
if (localDeviceTime.isAfter(currentAlarmTime)) {
calender.add(Calendar.DATE, 1);
System.out.println("The time of day for the alarm has already past rescheduling for tomorrow");
}
// For the different versions of operating system set off a single shot alarm
if(Build.VERSION.SDK_INT >= 23) {
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calender.getTimeInMillis(), pIntent);
} else if(Build.VERSION.SDK_INT >= 19) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
}
}
I am assuming the problem is going to be with these lines here:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
And this line when setting the alarm:
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
But I am not sure why that would be not starting exactly on the change of minute.
Thanks for your help

Calendar.getInstance() returns a Calendar instance set to the current time (and date).
You're setting the HOUR_OF_DAY and MINUTE fields of this Calendar instance, but the SECOND field still have its initial value, that is the cause of this behaviour.
Change the following:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
To this:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);

Related

convert time of format(00:00) to millisecond

I am developing an Alarm clock type app which shows notification bar at the time which is set by the user using Time picker widget, for this I use Alarm Manager as follows:
TimePicker tm=(TimePicker)findViewById(R.id.timePicker);
int hour=tm.getHour();
int min=tm.getMinute();
Intent intent = new Intent(this, my_broadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(),234324243, intent, 0);
Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
int h=cSchedStartCal1.HOUR;
int m=cSchedStartCal1.MINUTE;
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,X, pendingIntent);
I want to know the Value of X so to Notify at that time
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
calendar.setHour(HOUR_OF_DAY, hour);
calendar.setMinute(MINUTE, minute);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Also, your app won't work properly on android marshmallow and later. You should consider using AlarmManager along with setAlarmClock() as described here
then try like this
Calendar calendar = Calendar.getInstance();
calendar.set(year, month,day,
hour, minute, 0);
long startTime = calendar.getTimeInMillis();
here set year as current year, current month, current day, selected hour and minute

repeating Manager alarms every week

I want to do a specific task each week .
So I used an Alarm Manager
when user click button alarmManager running
blow code Works properly:
public void set_alarm(int reapte)
{
Context context=getBaseContext();
Calendar calendar=Calendar.getInstance();
// calendar.set(Calendar.HOUR_OF_DAY, 20); // For 1 PM or 2 PM
//calendar.set(Calendar.MINUTE, 41);
Intent intent= new Intent(context, MyService.class);
intent.putExtra("size", reapte);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getService(context, 0,
intent ,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY*7, pi);
}
The problem that I have.
When the user clicks. Alarm Manager runs the same moment.
And next week will run.
I want to first run the alarm from next week
.sorry My language is not good
Try this
you can set the day from which alarm will begin.
calendar.set(Calendar.DAY_OF_WEEK, 1);
where sunday=1 and so on to sat=7
and set the time 2 min before from current time when alarm was set,so it will same day of next week
I finally find the answer
WEEK_OF_MONTH values is between 1 and 4.
so
I use below code:
int week=calendar.get(Calendar.WEEK_OF_MONTH);
int day=calendar.get(Calendar.DAY_OF_WEEK);
int hour=calendar.get(Calendar.WEEK_OF_YEAR);
int minute= calendar.get(Calendar.MINUTE);
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE,minute);
int next_week=week+1;
if(next_week>4)
next_week=1;
calendar.set(Calendar.WEEK_OF_MONTH, next_week);
//other code

Alarm Manager Firing Immediately after real time has been passed

I want to set and cancel an Alarm for a particular time. I am doing the same using the TimePicker using the following code.
public void setRecurringAlarm(int randomTimer,long mills, int i){
Intent intent = new Intent(CreateAlarmActivity.this, AlarmReceiver.class);
intent.setData(Uri.parse("timer:" + i));
PendingIntent pendingIntent = PendingIntent.getBroadcast(CreateAlarmActivity.this, 1253, intent, PendingIntent.FLAG_CANCEL_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,mills,
pendingIntent);
Toast.makeText(CreateAlarmActivity.this, "Alarm "+i+" isSet", Toast.LENGTH_LONG).show();
}
Note:-Suppose I set the alarm for 10:00 PM. It works fine for 10:00 PM. But when I again run the same code (after 10 PM) i.e once the time has been passed on which the alarm has been set and I recall that code (to reset the alarm), it starts running immediately. Why it is so ? I am unable to get where I am a wrong.
You can check if the alarm time is before the current time or not. If so, then set the alert time for the next day (if you want to fire alarm at least once, or want to set Repeating alarm).
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
if (System.currentTimeMillis() > cal.getTimeInMillis()) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
public void scheduleAlarm() {
// time at which alarm will be scheduled here alarm is scheduled at 1
// day from current time,
// we fetch the current time in milliseconds and added 1 day time
// i.e. 24*60*60*1000= 86,400,000 milliseconds in a day
// Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 20);
cal.add(Calendar.MINUTE, 00);
cal.add(Calendar.SECOND, 00);
Intent intent = new Intent(CreateAlarmActivity.this, AlarmReceiver.class);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
PendingIntent.getBroadcast(this, 1, intentAlarm,
PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Scheduled ", Toast.LENGTH_LONG)
.show();
}
Hope this will help you
2 things:
If you are "recalling that code" by calling setRecurringAlarm(randomTimer, mills, i) using the same value for the mills parameter, then the time for the alarm will be in the past and it will trigger immediately (if you schedule an alarm with a trigger time in the past, the alarm triggers immediately).
Please remove | Intent.FILL_IN_DATA from the call to PendingIntent.getBroadcast(). It doesn't belong there as this parameter should contain only PendingIntent flags and it may do some damage.

Send Intent Each Day Using AlarmManager?

I want the following:
The AlarmManager sends a daily intent to my AppWidgetProvider at midnight EXACTLY.
The Alarm must wake up the device.
Here is how I did it:
In my AppWidgetProvider subclass:
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d(LOG_TAG, "Widget Provider enabled. Starting timer");
//Setting the Calender object to midnight time.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 0);
calendar.add(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR, 0);
//The fired Intent
Intent intent = new Intent(CLOCK_WIDGET_UPDATE); //custom intent name
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000*60*60*24, pendingIntent);
}
Although I think I did it right, this code doesn't work!
To test it, I changed 1000*60*60*24 with 10000 (10 sec) and updated a textView in the widget with random number. The random number never changed.
Any help will be appreciated !
I finally found the problem !
It seems that I need to set the AM_PM value too. When I print the calender using getTime().toLocaleString(), it was set to the next day but 12 hours later (12:00:00 PM).
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
This is a perfect setup for the Calender instance that will set the alarm to the next day at midnight (i.e. the nearest midnight). You just pass it to the setRepeating() method.
Thanks #CommonsWare
First, what you want is not strictly possible. Android is not a RTOS; you may not get control "at midnight EXACTLY"
Second, your Calendar object represents a time in the past (unless it happens to be midnight at the time you are executing that code, in which case it represents the present). Try a start time in the future.

Android: AlarmManager to trigger an event every day

Im trying to make an app, that would remind me of an event at the same time every day. For this purpouse Im using an AlarmManager.
This is how I register an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", cursor.getInt(0));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), cursor.getInt(0), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
This is how I unregister an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", id);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
This is how I edit an event:
Intent alarmIntent = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntent.putExtra("id_bundle", id);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
Intent alarmIntentNew = new Intent(getApplicationContext(), ReminderAlarm.class);
alarmIntentNew.putExtra("id_bundle", id);
PendingIntent pendingIntentNew = PendingIntent.getActivity(getApplicationContext(), id, alarmIntentNew, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntentNew);
I tried using setRepeating with an interval 24*3600*1000 but that didnt seem to work, so I decided to unregister the event when it triggers and register it again. This I do in the ReminderAlarm activity that starts after the alarm triggers.
Also, whenever I start the application, I register all the events again using the block of code I posted to register an event. I pull data about what events and what times to register them from a database.
My problem is that this doesent work either. When I add an event to a time T+1 minute, it triggers correctly. But the next day, it doesent.
Could it be perhaps because I give the calendar instance wrong data? I have quite a long function that sets up the calendar instance after an event triggers and before I send this calendar instance into the AlarmManager instance. In this function I chech for things like new month, new year etc and set calendar fields (year, month, day_of_month, hour_of_day, minute). Is this even necesary? Does calendar.getTimeInMillis() consider all those fields or just hour_of_day and minute? Do you see what might be wrong? Thank you!
Why not do this to get the time.
Calendar cal = Calendar.getInstance();
int now = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_MONTH, 1);
int tomorrow = cal.getTimeInMillis();
I think the calender could be what's doing it.
PS. I was the person who suggested the alarm in the last post :P Looks like you are doing good.
That useful code:
//Create a calendar.
Calendar cal = Calendar.getInstance();
//Get the time in miilis since Jan 1st 1970 I think?
cal.getTimeInMillis();
//Set the calendar
cal.set(year, month, day, hour, minute, second);
//set a certain aspect of the calendar
cal.set(Calendar.<What you want to set>, <amount>);
//for example
cal.set(Calendar.DAY_OF_MONTH, 1);
//Add to the calendar, Calendar.DAY_OF_MONTH what ever you are using specifies the amount of milliseconds to add, for example a day is 100*60*60*24 milliseconds where a second is 1000 milliseconds.
cal.add(Calendar.<Day or month or what ever>, <amount>);
//for example
cal.add(Calendar.MONTH, 1);
//Take of the calendar
cal.add(Calendar.MONTH, -1);
//Get that value
int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
Hope you enjoy :)

Categories

Resources