AlarmManager set Alarm for Weekly Repeated On Selected Days - android

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

Related

How to set next day or after two day alarm

supposed today is Wednesday now i want to set alarm (8am) Thursday or Friday . i already tried many way but alarm is not triggering , here is code github-:https://github.com/JaberAhamed/alarmClock
Calendar calSet = Calendar.getInstance();
Calendar now=Calendar.getInstance();
calSet.set(Calendar.DAY_OF_WEEK,week);
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, minuts);
calSet.set(Calendar.AM_PM, formate);
calSet.set(Calendar.SECOND, 0);
if (calSet.before(now)){
Toast.makeText(context, "before ", Toast.LENGTH_SHORT).show();
calSet.add(Calendar.DAY_OF_WEEK,1);
}
alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
int pos = position+ week;
intent.putExtra("extra", "yes");
pendingIntent = PendingIntent.getBroadcast(context, pos, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pendingIntent);
As I research, You can use alarmManager with Calendar, It will solve your problem.
At first, you need to detect current day of week and you counting it more 1 or 2 day and set into your alarm
Calendar calendar = Calendar.getInstance();
int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
calendar.setTimeInMillis(System.currentTimeMillis());
calSet.set(Calendar.DAY_OF_WEEK, currentDayOfWeek + 2);
calSet.set(Calendar.HOUR_OF_DAY, 8);
calSet.set(Calendar.MINUTE, 0);
Hope it can help you!
Fix me if I have any wrong !

Using a Service with AlarmManager and Calendar to run task every specific hours doesn't work

I'm trying to write code that will run a task on specific hours every day.
I have an hours array that contains integers and I loop through it to set a repeating alarm from that hour and with an interval of a day.
If I just create the service and run the task every 10 seconds or something it does run the AlarmReciever but this code doesn't work after the addition of the Calendar API, what am I doing wrong?
AlarmManager alarmManager;
Calendar current = new GregorianCalendar();
Calendar calendar = new GregorianCalendar();
int[] hours = new int[] {14, 18, 21, 22};
public int onStartCommand(Intent intent, int flags, int startId) {
current.setTimeInMillis(System.currentTimeMillis());
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, 0);
for (int hour : hours) {
calendar.add(Calendar.DAY_OF_YEAR, current.get(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.DATE, current.get(Calendar.DATE));
calendar.set(Calendar.MONTH, current.get(Calendar.MONTH));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
return START_STICKY;
}
EDIT: I tried printing out calendar.getTimeInMillis() for every loop and here is the output
I/System.out: 2391249600000
I/System.out: 2643724800000
I/System.out: 2769962400000
I/System.out: 2833038000000
Lets say I take the two first numbers: 2643724800000 - 2391249600000 = 252475200000. 252475200000 / 1000 = 252475200. 252475200 / 60 = 4207920.
I'm pretty sure 4207920 minutes is more than one hour. Why is like this?
Okay, I found out how to fix my problem.
First of all thanks to #MikeL for suggesting to remove calendar.add(Calendar.DAY_OF_YEAR, current.get(Calendar.DAY_OF_YEAR)); because that did fix some stuff, but even after that it didn't work.
To fix the problem I changed PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, 0); to PendingIntent pendingIntent = PendingIntent.getBroadcast(this, hour, intent1, 0);
Basically changing the 0 to hour so that every pending intent is different.

How to set Alarm to multiple dates at same time once

I am having following code to set the alarm.It is working for single date.But in my application user can select multiple dates and and when he/she selects the multiple date i have to set the alarm on each selected date on selected time.eg- if user selects three dates 11,12,13 and selects time 8am then the alarm should ring on 11,12,13 on 8am following is my code where i am setting alarm for single date
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.DAY_OF_MONTH, 11);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Use different request code for each PendingIntent like this
final int requestId = (int) System.currentTimeMillis();
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, requestId, myIntent, 0);

Alarm Manager fails to Trigger Alarm if date is added to calender

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

Implementing an alarm every 5 days, code correct?

I am trying to set an alarm every 5th day of the week and the 24th hour of that day.
Here is the code i am using. Ive been reading over the Calendar and AlarmManager docs, a
and here is what i have came up with.
String alarm = Context.ALARM_SERVICE;
//Alert for game covers
am = (AlarmManager)context.getSystemService(alarm);
calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 5);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent Aintent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, Aintent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, pi);
Is this correct for what i want to do?
To get a Calendar instance, that points to a date 5 days in the future, you take the current date and add 5 days like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 5);
Then you set your alarm:
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);

Categories

Resources