I am try to set an alarm to trigger a service every 12 months.
so I tried the following:
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.MONTH, calendar.get(Calendar.MONTH) + 12);
Log.e("calendar", "= " + Calendar.MONTH + " " + calendar.get(Calendar.MONTH) + 10);
long sdl = calendar.getTimeInMillis();
Intent myIntent = new Intent(mContext,
AlarmAlertBroadcastReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) mContext
.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, sdl, pendingIntent);
The above is not triggering every 12 months, I tried every 10 months it triggered? How?
If you want something to be triggered every twelve month then add a year to Calendar instance. Something like this -
calendar.add(calendar.YEAR, 1);
Also, if you really want to stick with adding months this is the solution -
calendar.add(calendar.MONTH, 12);
Note - I am using add() not set().
Hope it helps.
Related
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 !
how can i make the alarm start in the specific date in the future ?
the code below should start alarm every month in a specific day .
what happen is that the alarm start immediately and not in the given date also it is repeated next month in the day that started at and not in the given day.
for exemple :
i set day = 15 , month = 3 , year = 2014.
Today is 10 / 2 /2014.
i start the alarm today and it's strat immadiatly;
then next month it's start at 10/3/2014 and not at 15/3/2014.
can u help me plz...
Calendar calendar3 = Calendar.getInstance();
calendar3.add(Calendar.DAY_OF_MONTH , day);
calendar3.add(Calendar.MONTH , month);
calendar3.add(Calendar.YEAR , year);
Intent intent3 = new Intent(context, RepeatedTransaction.class);
intent3.putExtra("transType" , transType);
intent3.putExtra("transid" , transid);
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, transRepeatedId + 2, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am3 = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am3.setRepeating(AlarmManager.RTC_WAKEUP, desireHour, AlarmManager.INTERVAL_DAY * calendar3.getActualMaximum(Calendar.DAY_OF_MONTH), pendingIntent3);
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.
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);