I am trying to create the Alarms in my application using AlarmManager.
I am able to set multiple alarms, but if DATE parameter is added to Calender, the alarms are not at all triggered. Following is my code
Intent intent = new Intent(this, OneShotAlarm.class);
/*Pass the task row ID as the Unique ID for Pending Intent*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), (int) rowid , intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
calendar.clear();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMinute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long timeSet = calendar.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, timeSet, pendingIntent);
If I add the Date parameters to Calender as
calendar.add(Calendar.DAY_OF_MONTH, mDay);
calendar.add(Calendar.MONTH, mMonth);
calendar.add(Calendar.YEAR, mYear);
The alarms are not triggered. I have to schedule a event at a future date. Please suggest what I am missing. Thanks for the help!!
P.S. I am taking the date and time from Date & Time dialog picker
I have implement AlarmManager many times, Following technique will help you.
calculate your alarm time in milliseconds for example you want to set alarm after 10 minutes then 10*60*1000 millisecond after current time.
Add your calculated time in current millisecond
Example
long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);
PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int)fireTime , ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);
Above example works perfect.
Thank You,
Ketan's answer is good but there is an error.
long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);
PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int) requestCode, ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);
However you don't want "fireTime" in the PendingIntent. You should have a request code. Which is a code you create to identify your pending intents. It works in Ketan's case because he is always using the same time. But if you change the time, you will end up with two different intents.
see https://developer.android.com/reference/android/app/PendingIntent.html
Related
I am Creating Alarm Application with Setting Alarm of Multiple Days i.e Repeating Alarm.My Android Alarm Application View Like This,
i Have Done Code for this,
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(ALARM_ACTION_NAME);
alarmIntent.putExtra("AlarmID", m_alarmId);
PendingIntent alarmPI = PendingIntent.getBroadcast(this, m_alarmId, alarmIntent, 0);
//listofred is a ArrayList of int items.contains int valye for selected days...for My Example listofred:3,4,5,7
for (int i = 0; i < listOfred.size(); i++) {
// for alarm ...
calNow = Calendar.getInstance();
calSet = (Calendar) calNow.clone();
int day = calSet.get(Calendar.DAY_OF_WEEK); //current day...for example.13 dec 2014 - sat so, day = 7
calSet.set(Calendar.DAY_OF_WEEK, listOfred.get(i));
calSet.set(Calendar.HOUR_OF_DAY, time_picker.getCurrentHour());
calSet.set(Calendar.MINUTE, time_picker.getCurrentMinute());
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), (DateUtils.DAY_IN_MILLIS)*7,
alarmPI);
//parameter long intervalMillis.....(DateUtils.DAY_IN_MILLIS)*7 so that it will repeat after each 7 days...
}
My Problem is When i run this code it will set repeat alarm for only Saturday(i.e last object in listofred Arraylist) every time it set alarm for last object in Arraylist.
I know it is quite late to answer this, but isn't it because of the same pending intent being passed to each alarm event.
Perhaps this might help.
PendingIntent alarmPI = PendingIntent.getBroadcast(this, m_alarmId, alarmIntent, PendingIntent.FLAG_ONE_SHOT);
https://stackoverflow.com/a/3009690/1111127
I am using the alarmmanger class set method for setting the alarm. i
takes the input from user through timepicker dialog for setting the
alarm.alarm work fine when i set specific hour and minutes but
application is not following the am/pm logic. mean if i set the alarm
10:31 am while in system time is 10:30 pm then alarm invoke at 10:31 pm not at 10:30 am.anyone tell me whats the reason?
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, hourOfDay);
time.set(Calendar.MINUTE, minute);
time.set(Calendar.SECOND, 5);
//time.set(Calendar.AM_PM);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(), pendingIntent);
you need to set the time with the time format using SimpleDateFormat or DateFormat class which will give the date based on the date-time string and get that value using getTime and set into the alarm in set method.
Or you can do another way get the alarm time and check whether it's am or pm and set this attribute into the calendar object with time and date and get millisecond value from the calendar set into the alarm.set() method.
Hope you get some idea/suggestion
Edit
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); // this is you format
Date d = sdf.parse("19/12/2011 03:47:00 pm"); // this string getting you from your timepicker
Intent myIntent = new Intent(youractivity.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(youractivity.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, d.getTime(), pendingIntent);
Note: you need to put into the try and catch block parse part it may be cause error during parsing time
I used this method in setting alarm:
/**
* Set the Alarm
*
* #param context the activity context
* #param id the alarm ID for this app
* #param hour the alarm hour
* #param minute the alarm minute
* #param timeZone the timezone am = Calendar.AM or pm = Calendar.PM
*/
public static void setAlarm(Context context, int id, int hour, int minute, int timeZone) {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.AM_PM, timeZone);
Intent intent = new Intent(context, PopupActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, id, intent, 0);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 10, pIntent);
}
For the timezone parameter use either am = Calendar.AM or pm = Calendar.PM
Change the below line:
time.set(Calendar.HOUR, hourOfDay);
to
time.set(Calendar.HOUR_OF_DAY, hourOfDay);
I am using this code to launch an Alarm.
The alarm is set in an Activity that the user can launch.
//Setting alarm to fire off NEW_GAME intent every 24 hours.
String alarm = Context.ALARM_SERVICE;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND, 0);
Log.i("Test", "Current time: " + System.currentTimeMillis() );
Log.i("Test", "Calendar time: " + calendar.getTimeInMillis() );
int currentDate = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, currentDate+1);
Log.i("Test", "Calendar time with a day added: " + calendar.getTimeInMillis() );
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
Intent intent = new Intent("NEW_ITEM");
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender);
I was told i need to supply a uniqu id so that the alarm doesnt over writte each other where getBroadcast() is.
The problem is how do I do this when the user can open the Activity as many times as they want?
Also if I supply a unique ID each time this means it could possibly set 5 of the same ALARMS because of the unique id's.
How or what is the best way to get around this?
you could always just use the unix timestamp of your target time as the unique id. that way, alarms for the exact time WILL override each other, while all other alarms will stay seperate
[EDIT:] Here is some example code:
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
Intent intent = new Intent(String.valueOf(calendar.getTimeInMillis()));
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender);
I am using this code to set a Alarm everyday for 8 oclock the next day.
I am setting this alarm in an activity that can be opened based upon the user.
//Setting alarm to fire off NEW_GAME intent every 24 hours.
String alarm = Context.ALARM_SERVICE;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND, 0);
Log.i("Test", "Current time: " + System.currentTimeMillis() );
Log.i("Test", "Calendar time: " + calendar.getTimeInMillis() );
int currentDate = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, currentDate+1);
Log.i("Test", "Calendar time with a day added: " + calendar.getTimeInMillis() );
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
Intent intent = new Intent("NEW_ITEM");
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender);
My only question is..Lets say at 10:00 o clock today am. i open the activity that alarm is set for tomorrow..Lets say i open the activity again at 12:00 am mid-night, will the alarm set earlier that day be overr written by the current alarm being set?
If you use the same request number (second parameter) while creating the PendingIntent object
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
then it will overwrite the current PendingIntent and hence will replace the current Alarm.
It will also depend on what you pass as the last parameter to it. Possible values given in the constants section here.
In Android we set an alarm by setting the time until it goes off in milliseconds. Is there an easy way to find how many milliseconds there are until a certain time (hh:mm) or do I just have to calculate it mathematically?
Thanks!
Save your current time in milliseconds as
Calandar calendar = Calendar.getInstance();
Long currenttime = calendar.getTimeInMillis();
Long settime= <your set time in milliseconds>;
Here you can calculate the difference as follows:
Long differencetime = settime - currenttime;
int dif=(int)differencetime/1000;
Here you can set the time in calendar:
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + dif);
Here you can set the alarm for the settime.
AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi1);
Check out the first argument for AlarmManager.set(): With RTC/RTC_WAKEUP, you can specify a fixed time rather than an elapsed time.
That said, if you need to use the elapsed time, it's pretty trivial to calculate the number of milliseconds that need to elapse. Worst case, you could use the Calendar and/or Date classes.
Date now = new Date(), b = new Date(year, month, day, hour, min);
b.getTime() - a.getTime();
And here's another way to get the time in milliseconds to a certain time:
Calendar c = Calendar.getInstance();
c.set(c.YEAR, c.MONTH, c.DAY_OF_MONTH, 17, 1, 0); // 5:01 pm
long alarmTime = c.getTimeInMillis();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 1000 * 60 * 60 * 24, pendingIntent); // Millisec * Second * Minute * Hour // Same time every day
RTC_WAKEUP allows the alarm to still activate when the phone is asleep. Use RTC if you want to wait until the user wakes up the phone themself.
Use am.set() if you don't want the alarm to repeat
Let me know if this helps.