android repeat alarm from next sunday in this year - android

my repeating alarm start first time at sunday regardless time (if past) here is the problem , in next sundays alarm repeat correctly , i want start alarm FROM NEXT SUNDAY .. this is my code :
AlarmManager alarmMgr = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(activity, Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendarStart = Calendar.getInstance();
calendarStart.set(Calendar.DAY_OF_WEEK, 1 );
calendarStart.set(Calendar.HOUR_OF_DAY,8);
calendarStart.set(Calendar.YEAR,calendarStart.get(Calendar.YEAR));
calendarStart.set(Calendar.MINUTE, 0);
calendarStart.set(Calendar.SECOND, 0);
Toast.makeText(activity, calendarStart.getTime()+"",Toast.LENGTH_LONG).show();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendarStart.getTimeInMillis() ,24*60*60*1000, pendingIntent);

my repeating alarm start first time at sunday regardless time (if past)
Check if calendarStart is in the past and if it is add seven days:
if(calendarStart.compareTo(Calendar.getInstance()) < 0) {
// calendarStart is in the past, use next Sunday
calendarStart.add(Calendar.DAY_OF_YEAR, 7);
}
Toast.makeText(activity, calendarStart.getTime()+"",Toast.LENGTH_LONG).show();

Related

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

Run Alarm In Selected Days

I want to start alarm when i have selecting the days,hours,and minutes.
This is my code so far :
public void startAt() {
Intent alarmIntent = new Intent(backgroundApplication.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(backgroundApplication.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK,calendar.WEDNESDAY);
calendar.set(Calendar.HOUR_OF_DAY,7);
calendar.set(Calendar.MINUTE,35);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 5, pendingIntent);
}
For the example i set the DAY_OF_WEEK is today(friday) it works great. But when i changed it into Wednesday, or other day before friday it still running.
Is there any other way to start in selected days ?
According to the documentation http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29, the 2nd parameter is the trigger time in milliseconds and if you set it to a time in the past, the alarm will trigger immediately. If you want the alarm to only start from next Wednesday, then you have to advance your Calendar time to the next Wednesday with calendar.add(Calendar.WEEK_OF_MONTH, 1). By the way, in your code, you don't need to have calendar.setTimeInMillis(System.currentTimeMillis()).Calendar.getInstance() will return the current time by default. You should also use static field Calendar.WEDNESDAY.

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.

How do I register the AlarmManager to repeat selected days?

I'm creating an alarm application.
In my application, the user can select the days to fire the alarm, such as Sunday, Monday, so I used the Calendar class and AlarmManager to register multiple alarms.
If the selected day or days are less than the current day of the week, the alarm fires immediately.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, getDayint); //sunday = 1
calendar.set(calendar.HOUR_OF_DAY, gethour);
calendar.set(calendar.MINUTE, getmin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
long TIM = calendar.getTimeInMillis();
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, TIM, sender);
//repeat weekly
am.setRepeating(am.RTC,TIM, AlarmManager.INTERVAL_DAY*7, sender);
By using PendingIntent.FLAG_UPDATE_CURRENT you say that you like to have only one such alarm - so latest one wins - only one alarm weekly. And if TIM lies in past, alarm is fired immediately ( and then repeating ) So far - everything works as designed.
You may check whether TIM ( by the way, it is variable, and concention is that they shall be not uppercased like constants ) is less than System.currentTimeMillis() and add one week to it in this case

How can i Repeat the Alarm in android for only Monday, Tuesday and Friday

How can i Repeat the Alarm in Android for only Monday, Tuesday and Friday.
Intent myIntent = new Intent(getApplicationContext(), x.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//Calendar calender = new GregorianCalendar();
Calendar calender = Calendar.getInstance();
calender.setTimeInMillis(System.currentTimeMillis());
calender.set(Calendar.HOUR_OF_DAY, hours);
calender.set(Calendar.MINUTE, ireminder.getMin());
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);
calender.set(Calendar.DAY_OF_WEEK_IN_MONTH,3);
calender.set(Calendar.DAY_OF_WEEK_IN_MONTH,2);
calender.set(Calendar.DAY_OF_WEEK_IN_MONTH,6);
alarmManager.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pendingIntent);
But it is not repeating for 3 and 6 days (Monday,Tuesday and friday)
Can you guys Helpme???
You need to set 3 separate alarms, or make your alarm receiver smart enough to schedule the next alarm when the previous fires. In your case above, looks like only the value for "6" would be used, since it is the last value set.

Categories

Resources