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
Related
I would like to configure alarm manager for a different timezone. I would like the alarm to trigger a notification whenever 10:20 NY time has reached. Is it possible to code like below?
private void configureDailyAlarm(Context context) {
TimeZone USTimeZone = TimeZone.getTimeZone("America/New_York");
Calendar USCalendar = Calendar.getInstance(USTimeZone);
USCalendar.set(Calendar.HOUR_OF_DAY, 10);
USCalendar.set(Calendar.MINUTE, 20);
Intent alarmReceiverIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, USCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
Use this method:
public int getTimeFromNY() {
TimeZone USTimeZone = TimeZone.getTimeZone("America/New_York");
Calendar USCalendar = Calendar.getInstance(USTimeZone);
USCalendar.set(Calendar.HOUR_OF_DAY, 10);
USCalendar.set(Calendar.MINUTE, 20);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); // Current date
return USCalendar.getTimeInMillis() - calendar.getTimeInMillis();
}
Then plug that into AlarmManager
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeInMillis() + USCalendar.getTimeFromNY(), AlarmManager.INTERVAL_DAY, pendingIntent);
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 !
I want to fire an alarm in a particular day of the week. For example, every Monday at 4:30 pm.
The following code is what I have written for doing that, but it's not working. What am I doing wrong?
public void setAlarm(int day,int hour,int minit) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE,minit );
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.DAY_OF_WEEK, day);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Long alarmTime = cal.getTimeInMillis();
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmTime,
24 * 60 * 60 * 1000 *7 , pendingIntent);
}
Strangely, my Alarm Manger is calling the pending intent immediately, even though I have put a condition if the current time is greater than alarm time run one day later.
I have alarm Manger to trigger everyday at 10PM.
Intent startServiceIntent = new Intent(this, numbers.class);
this.startService(startServiceIntent);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(res.getString(R.string.hoursvalue))); //10
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.AM_PM, Calendar.PM);
cal.set(Calendar.MILLISECOND, 0);
long alarmtime = cal.getTimeInMillis();
Log.e("ALarm","Time"+alarmtime);
if (currenttime > alarmtime)
{
alarmtime = alarmtime + AlarmManager.INTERVAL_DAY;
Log.e("Current Time","Greater"+alarmtime);
}
if (currentapiVersion >= 19)
{
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(this, ServiceForLoadingOnlineNumbers.class);
PendingIntent pi = PendingIntent.getService(this, 741258963, intent2, 0);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmtime, AlarmManager.INTERVAL_DAY, pi);
}
else if (currentapiVersion >= 16 && currentapiVersion < 19)
{
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent3 = new Intent(this, ServiceForLoadingOnlineNumbers.class);
PendingIntent pi = PendingIntent.getService(this, 741258963, intent3, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmtime, AlarmManager.INTERVAL_DAY, pi);
}
The Log shows the next day in milliseconds correctly but triggers immediately. What could be the issue here?
Try to use this way and let me know if you face any issues
Date dat = new Date();// initializes to now
Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_now.setTime(dat);
Toast.makeText(mContext, "" + cal_now.getTime(), Toast.LENGTH_LONG)
.show();
;
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY, 13);// set the alarm time
cal_alarm.set(Calendar.MINUTE, 10);
cal_alarm.set(Calendar.SECOND, 10);
if (cal_alarm.before(cal_now)) {// if its in the past increment
cal_alarm.add(Calendar.DATE, 1);
}
I suggest checking if the alarm manager is null then canceling the alarm before setting the alarm this took me some time example
if(alarmManger != null){
alarmManger.cancel(operation);
alarmManger.setRepeating(alarmType,alarmtime,AlarmManager.INTERVAL_DAY, operation);
}
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);