How to set next day or after two day alarm - android

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 !

Related

Calendar sets alarm for next day

I'm working with Calendar. I use code below to set an alarm. But!!! If I try to set an alarm on Monday(Calendar.Monday) the actual alarm will be set on Tuesday. Why does this happen? I also tried to use Calendar.getInstance(Locale.getDefault()) but it didn't work.
And this is how I set an alarm for Sunday:
setAlarm(Calendar.SUNDAY, h, m, k, y);
My setAlarm() method:
public void setAlarm(int dayOfWeek, int hour, int minute, int position, int y) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,MyReceiver_Alarm.class);
Long alarmTime = calendar.getTimeInMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), position , intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.e("Point_1", "Time is " + calendar.getTime());
}
Thank you.
0 Sunday
1 Monday
2 Tuesday
DayOfWeek is 0-based, and it starts from Sunday.
So if you're passing 2, thinking that it's 1-based, you're getting Tuesday instead.
The parameter Calendar.DAY_OF_WEEK starts from SUNDAY, so if u want to set alarm for monday set the value of int dayOfWeek to 2 . refer image link

How to set alarm manager in android M

I use setAlarmClock but delay from the set time.
Mycode
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra(AppConstant.REMINDER, note.convertToString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
note.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(getTargetTime().getTimeInMillis(),pendingIntent),pendingIntent);
}
Calendar function
private Calendar getTargetTime() {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.DAY_OF_MONTH, sDay);
calSet.set(Calendar.HOUR_OF_DAY, sHour);
calSet.set(Calendar.MINUTE, sMinute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
return calSet;
}
I test with pure android M. And time delay about 3-5 minutes.
Thankyou and Sorry for bad english

Android - how to set alarm X days before specific date

I have seen a lot of tutorials for creating an AlarmManager and bringing up a notification at a specific date, but I was wondering how to do it for X days before.
For example, if I have an event on January 8th, 2014 and I want to set it a week before to Jan 1st, 2014, I would just set the alarm as this:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.DAY_OF_MONTH, Day-7);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
However what if the event was on January 4th, 2014, then I would want to set the date to December 28th, 2013. Is there a function that can do this? Or do I have to write a lot of cases?
the event was on January 4th, 2014:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 4);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long eventTime=calendar.getTimeInMillis();//Returns Time in milliseconds
long oneDay=AlarmManager.INTERVAL_DAY;//Converts 24 Hrs(1 Day) to milliseconds
int noOfDays=4
long reminderTime=eventTime-(noOfDays*oneDay);//Time in milliseconds when the alarm will shoot up & you do not need to concider month/year with this approach as time is already in milliseconds.
//Set alarm
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
Hope this helps!
get your current time in milliseconds and subtract the no. of days from it,here 1 day= 24*60*60*1000 = 86400000
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, (calendar.getTimeInMillis()-(no. of days * 86400000)), pendingIntent);

AlarmManager firing alarm past the time it was set on the same day, setRepeating

So basically I have this code, time returns 24hour time and repeats the alarm daily.
public setAlarm(String time, Context context){
String[] strTime;
strTime = time.split(":");
int hour, min, sec;
//set when to alarm
hour = Integer.valueOf(strTime[0]);
min = Integer.valueOf(strTime[1]);
sec = 0;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, sec);
//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 19248, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
Anyways, the problem is when I set the alarm 9am while the time is 9:10am, the alarm will go off. Why? I want it not to alarm if it is set past the system time. Ex. set the alarm at 9am while the system time is 9:10am
I got it working now. I added a checker of the alarm time and current time.
public setAlarm(String time, Context context){
String[] strTime;
strTime = time.split(":");
int hour, min, sec;
//set when to alarm
hour = Integer.valueOf(strTime[0]);
min = Integer.valueOf(strTime[1]);
sec = 0;
long _alarm = 0;
Calendar now = Calendar.getInstance();
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, min);
alarm.set(Calendar.SECOND, sec);
if(alarm.getTimeInMillis() <= now.getTimeInMillis())
_alarm = alarm.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm = alarm.getTimeInMillis();
//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 19248, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, AlarmManager.INTERVAL_DAY, pendingIntent);
}
The accepted answer is wrong as I tried it and then figured out the solution.
It is wrong because, as you can see here:
Calendar now = Calendar.getInstance();
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, min);
alarm.set(Calendar.SECOND, sec);
"Calendar now" and "Calendar alarm" will always be the same no matter what because they are doing the same thing almost at the exact same spot in the code so the Calendar.getInstance() wil always be the same.
The solution is this
long _alarm;
Calendar now = Calendar.getInstance();
long oldtimer = now.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, Hours2int);
cal.set(Calendar.MINUTE, minutes2int);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
//Calendar alarm = Calendar.getInstance();
long newtimer = cal.getTimeInMillis();
if(newtimer < oldtimer) {
//do the thing
}
Use setInexactRepeating instead of setRepeating
Calendar cal= Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);

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