this is how I set my alarm clock:
//all this inside a onClickListener
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 30);
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.YEAR, 2020);
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
intent.putExtra(AlarmClock.EXTRA_HOUR, Integer.parseInt(str1));
intent.putExtra(AlarmClock.EXTRA_MINUTES, Integer.parseInt(str2));
intent.putExtra(AlarmClock.EXTRA_MESSAGE, title.getText().toString());
intent.putExtra(AlarmClock.EXTRA_DAYS, cal.get(Calendar.DAY_OF_MONTH));
intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
startActivity(intent);
All this set very well the app alarm clock on my device regarding the time, but not for the date.
Example:
Current time: 12:00 Monday
Time from my code: 13:00 Saturday
The alarm clock sets to play (the ringtone) in 1 hour
Current time: 12:00 Monday
Time from my code: 11:00 Tuesday
The alarm clock sets to play (the ringtone) in 25 hours
I dont know how to use in particular this line of code:
intent.putExtra(AlarmClock.EXTRA_DAYS, cal.get(Calendar.DAY_OF_MONTH));
Thanks in advance
You cannot set the an alarm for a specific date with an AlarmClock provider.
AlarmClock.EXTRA_DAYS is for repeating alarms. You could use something like Calendar.SUNDAY and it would ring every Sunday.
If you want more control over an alarm, you have to program it yourself. Look into the AlarmManager class. It allows you to schedule your application to be run at some point in the future.
Related
I am creating a alarm clock application for android. I am facing certain issue Such as :-0
After searching entire internet. I didn't get any good solution.
How to set repeat alarm for user selected days? For example if user selected to set an alarm at Sunday and Monday on 9:00 AM. I tried certain solution from the internet but nothing works for me. This is my code
calendar = Calendar.getInstance();
Intent intent = new Intent(context,BROADCAST_RECEIVER.class);
calendar.set(Calendar.DAY_OF_WEEK, WEEK_DAY);
calendar.set(Calendar.HOUR_OF_DAY,HOURS);
calendar.set(Calendar.MINUTE, MIN);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
PendingIntent pendingIntent = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getBroadcast(context, sharedPref.GET_ALARM_CODE(), intent, PendingIntent.FLAG_MUTABLE);
}
else {
pendingIntent = PendingIntent.getBroadcast(context, sharedPref.GET_ALARM_CODE(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), (DateUtils.DAY_IN_MILLIS)*7 , pendingIntent);
dbHelper.alarmDao().AddAlarm(new AlarmDatabase(calendar.getTimeInMillis(),AM_PM,DAYS,sharedPref.GET_ALARM_CODE(),"ON",notes,alarm_time,need_to_show));
}
How to calculate specific time in milliseconds for the user selected days ? I used this code.
calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, WEEK_DAY);
calendar.set(Calendar.HOUR_OF_DAY,calender_hours);
calendar.set(Calendar.MINUTE, calender_minutes);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
By the time in am writing this question it's Friday here. I to calculate time from Friday 6:31 to Saturday 6:31 in milliseconds but get 1660933800000 this much milliseconds. Which is equivalent to 461370.5 in hours.
How to know that user selected past time ? So we can set alarm for the next day according to user selected hour and minutes.
I am thinking about one approach to solve 1 problem. I don't know is this good or not. What if on broadcast receiver I took current alarm schedule from local database and set alarm for next day using setexact() ? For example user selected Monday and Wednesday. And when alarm trigger on Monday then in receiver I check next alarm schedule which is Wednesday and set alarm for Wednesday. And this cycle repeats. Is this good ?
Thanks in advance for help.
I have had several frustrations with setting alarms in Android. I have tried setting repeating/non-repeating alarms and exact/inexact alarms but it does not matter, if the alarm is ever set for a time in the past, it executes as soon as it is set. I have tested this as far back as setting an alarm for 5 hours in the past and is still executes immediately.
For example:
The time is 7 AM and I set an alarm to execute at 2 AM. This is obviously meant for the next time the clock reads 2:00 AM but it does not matter, the alarm goes off at 7 AM, right after it is set.
The code below should select a random time between 1:00 AM and 3:59 AM to set/execute the alarm for the next calendar day and then the logic circles back around to set itself again after execution. The alarm will execute repeatedly, forever.
int randomHour = new Random().nextInt((3 - 1) + 1) + 1;
int randomMinute = new Random().nextInt((59 - 1) + 1) + 1;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, randomHour);
calendar.set(Calendar.MINUTE, randomMinute);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Questions:
At what point does Android stop executing alarms in the past?
Is there any way to stop this?
"This is obviously meant for the next time" Does not work for computers, they will do exactly what you tell them to do.
calendar.getTimeInMillis() returns the number of milliseconds since January 1, 1970 at 00:00:00 GMT. You need to specify not just the time but also the date that you want the alarm to go off. Instead you are always calling calendar.add(Calendar.DAY_OF_MONTH, 1); (the first day of the current month)
I have followed the following link https://developer.android.com/training/scheduling/alarms.html.
RTC examples first one, to set an alarm for a specific time on all days. Even after following the same code, the alarm is not triggered at the time it is suppose to get triggered. Instead the notification gets triggered immediately after setting the time which is done with the help of a time picker. Following is my code snippet,
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent intentalarm = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentalarm);
Instead of setRepeating I also tried setInexactRepeating but there was no luck.I would also like to add that when I changed the
calendar.getTimeInMillis() and used SystemClock.elapsedRealtime() + 120 * 1000 the alarm triggered exactly after 2 minutes from the time it was set.
But when calendar.getTimeInMillis() is being used the intended working does not happen instead immediate triggering occurs.
Would be indeed very helpful if anyone can help me out find a solution.Only for a note, I could learn if alarm is set before current device time the alarm would be triggered immediately but that is not the case here.
NotificationReceiver.class is working fine as it is generated and appears on the title. But the time it appears is the cause of concern.
You are using both setTimeInMillis and hourofday and minute.
If you want your alarm to be triggered at 4:30 just add hourofday and minute.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 30);`
I could Identify the issue I was facing. I was using a TimePicker with 24 hour format. But I used SimpleDateFormat "hh:mm". What 'h' stands for is as follows "h->Hour in am/pm (1-12) ". So any alarm that I set at AM triggers correctly. When I set a time at PM since I had used 1-12 'h' format, alarm gets set for a AM time and since that time is already passed when compared to the device time the alarm/notification got triggered immediately.The right format to use was "HH:MM" where 'H' stands for "H->Hour in day (0-23)". This resolved my issue.
Checking the TimeinMillis on online epoch time converter helped me identify this issue.Once again thanks for helping me.
I am trying to execute a background process on my android app every day at around 09h10 am. This works fine, but problem is after the first alarm has been fired at 09h10 am, it gets re-fired after 10 - 20 minutes throughout the day. I just want it to fire once a day, and only at that specified time. My code that sets the alarm manager is below:
PendingIntent reviewsPendingIntent = PendingIntent.getBroadcast(this,0,new Intent(this,ReviewReceiver.class),0);
AlarmManager alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 10);
cal.set(Calendar.SECOND, 10);
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
long interval = 6000*1440;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),interval,reviewsPendingIntent);
I have set the interval to 8640000 which is a day(I believe) if not, please advise accordingly. Thank you
1000ms*60s*60m*24h = 86400000 so You missed one 0
You can use
PendingIntent reviewsPendingIntent = PendingIntent.getBroadcast(this,0,new Intent(this,ReviewReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,reviewsPendingIntent);
FLAG_CANCEL_CURRENT will prevent the alarm to be set multiple times
AlarmManager.INTERVAL_DAY = 1000 * 60 * 60 * 24
If you keep having some delay, it's because the sleep function is not precise. It's around the time you have set. Try with setExact and reschedule every day, doing the math by hand (initialTime + dayPassed * millsInADay) to have the best approximation.
If what Lindus has posted worked though, just forget it :) but I think that you'll have the same problem.
I want the AlarmManager to repeat a task at scheduled time (weekly)
I used the following code:
for (Integer day : daysList) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, PersonalUtils.getDigitalWeek(day));
c.set(Calendar.HOUR_OF_DAY, task.getHour());
c.set(Calendar.MINUTE, task.getMinute());
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
intent.putExtra("id", task.getId());
PendingIntent operation = PendingIntent.getService(
getApplicationContext(), requestCode, intent, 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 7, operation);
}
However the alarm it triggered prematurely.
For example: Assuming that it is 18:30 Wed now. I setup a task which should be triggered at 17:30 Tue next week but instead the alarm is triggered immediately
Can anybody tell me why?
You are trying to do an inexact alarm, which only allows for a few specific constants, INTERVAL_DAY, INTERVAL_FIFTEEN_MINUTES, etc. See Android Docs for more. Those constants are only supposed to be used for InexactRepeatingAlarms, but I see your doing a RepeatingAlarm.
You have a couple of choices, you can either set the alarm to trigger in exactly 1 week, or you can set it to trigger every day inexactly and only pay attention to it if the alarm occurs during the 7th day. To trigger exactly every 7 days from now, try this:
final long WEEK_IN_MILLIS= 604800000;
alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis()+WEEK_IN_MILLIS,
WEEK_IN_MILLIS, operation);
Note that I set it to first trigger in 1 week, then repeat every week after that. That should work for you.