Set repeat alarm for every year in android - android

I am building an alarm application in android.Till now i am able to create repeat alarm for every day,week and every month.
Now my requirement is to set repeat alarm for every year.
TIA

You can set alarm after every year, each time alarm is fired you can start a new service intent which will be set alarm for next consecutive year.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year + 1);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, alarmIntent);

This will help you to repeat alarm every year. Use DateUtils this will ease your work if you are working on Alarm App.
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alrarmTime, DateUtils.YEAR_IN_MILLIS, pendingIntent);

May be this is answer https://developer.android.com/training/scheduling/alarms.html
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
I think you can set alarm for next year, when time come alarmIntent run and set alarm for year + 1 , this can make it repeat every year
// Set the alarm to start at approximately next year
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year + 1);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, alarmIntent);

Related

AlarmManager start Alarm Instantly

I have created a form where user specify date and time and adds alarm. I take these date and time and creates Alarm using AlarmManager.
The problem is that as soon as I click on CreateRemainder button, Alarm is fired
I am using following method to pass date, time values to AlarmManager class.
int year= Integer.parseInt(splitDate[0]);
int month= Integer.parseInt(splitDate[1]);
int day= Integer.parseInt(splitDate[2]);
int hour= Integer.parseInt(splitTime[0]);
int minute= Integer.parseInt(splitTime[1]);
int second= 0;
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
cal.set(Calendar.DATE,day); //1-31
cal.set(Calendar.MONTH,month-1); //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR,year);//year...
cal.set(Calendar.HOUR_OF_DAY, hour); //HOUR
cal.set(Calendar.MINUTE, minute); //MIN
cal.set(Calendar.SECOND, second);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
I debug this and its getting the correct values, but it starts alarm immediately.
If i change the AlarmManager time to hard codded 10 seconds time, it starts remainder after 10 seconds.
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
But if i add 10 Seconds to the Calender, it still start displaying alarm instantly after setting it.
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+10000, pendingIntent);
Usually, if the alarm is being fired as soon as you set it, it means that scheduled time is behind current time... So, AlarmManager fires it imediatelly
I suggest to add debug messages to debug. Something like:
Log.d("DEBUG", "Time from device: " + Calendar.getInstance().getTimeInMillis() + " Time passed to alarm: " + cal.getTimeInMillis());
Then, you can check Current Millis website and check if both times are correct.. Check if cal is really a future date
Also, if you want to add a time to calendar, use as follows Calendar.add() method:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Use this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent);
}
If your alarm is getting called instantly than my guess is your cal's date is before today.

How to set alarm on specific day in calendar

I need to set alarm in my app to specific day that has been choosen from calendar ? As I have noticed Alarm doesn't have such method. I understnad that I can do this usign calendar and setting the time amount left for this date. But for example if user changes date,what happens ?
Maybe this question is stupid,sorry, I am newbie.
By using the AlarmManager class, you should be able to do this:
http://developer.android.com/reference/android/app/AlarmManager.html
Use public void set (int type, long triggerAtTime, PendingIntent operation) to set the time to fire it.
Use void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) to schedule a repeating alarm.
Here's how you can do this with your specified (date, hoursOfDay, minute, seconds and even millis):
AlarmManager mAlarmManager = (AlarmMAnager) Context.getSystemService(Context.ALARM_SERVICE);
Calendar mCal = Calendar.getInstance();
Calendar mCalSet = (Calendar) mCal.clone();
mCalSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalSet.set(Calendar.MINUTE, minute);
mCalSet.set(Calendar.SECOND, second);
mCalSet.set(Calendar.MILLISECOND, millis);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
mCalSet.add(Calendar.DATE, 1);
}
mAlarmManager.set(AlarmManager.RTC_WAKEUP, mCalSet.getTimeInMillis(), yourOperation);

alarm goes off as soon as i set it,please help me to remove it...?

here is a scenario:
suppose today right now the time is 9 am in the morning....
i set the alarm for 6 am daily,(note that the alarm time 6 am <9 am)
this is what happens
1 the alarm rings as soon as i set it(not required,i think that is because 6am <9am and hence the alarm rings for that day itslef as 6 am has already passed )
2 the alarm goes off for the next days too (required)
so how can i stop 1 to happen ???
here is the code i am using
Calendar calendar = Calendar.getInstance();
// 9 AM
calendar.set(Calendar.HOUR_OF_DAY, xhour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getBroadcast(Reminder.this, 0,
intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
Can you please try this:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, xhour);
calendar.set(Calendar.MINUTE, minutes);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);

set repeated alarm manager for every hour in android

I want to fetch location every hours in android . For that i use alarm manager and set repeated alarm for every hour and just want to write into file after fix time i.e at 8 AM and 12 PM .
I got a problem in setting alarm manager , while i set for every one hour but it execute in 1/2 hour .
on button click i start service :
serviceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(AutoMainActivity.this, TrackerService.class);
pendingIntent = PendingIntent.getService(AutoMainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ALARM_TRIGGER_AT_TIME,
3600000, pendingIntent);
//3600000 1hrs
finish();
}
});
And Service Class are as :
Tracker Service.class
String FINAL_STRING;
SharedPreferences pref;
static final int START_TIME = 8;
static final int MID_TIME = 12;
java.util.Date systemDates = Calendar.getInstance().getTime();
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if(hour == START_TIME)
{
edit.putString("smsdata", FINAL_STRING);
edit.commit();
//sendSms(START_TAG+pref.getString("smsdata", ""));
edit.putString("smsdata", "");
edit.commit();
}else {
System.out.println("currentdate:"+simpleDateFormat.toString());
System.out.println("current_time:"+currentTime);
Editor edit = pref.edit();
edit.putString("smsdata", pref.getString("smsdata", "")+FINAL_STRING+"#");
edit.commit();
if(hour==MID_TIME)
{
//sendSms(START_TAG+pref.getString("smsdata", ""));
generateNoteOnSD("\n"+START_TAG+pref.getString("smsdata", ""));
edit.putString("smsdata", "");
edit.commit();
System.out.println("mid time");
}
}
When i execute this the service start on every 30min. but i want on every 60min.
First, you really want to use one of the available constants, like INTERVAL_HOUR, with setInexactRepeating().
Second, setInexactRepeating() is inexact. Android reserves the right to flex the times of the alarms to coalesce events with other scheduled inexact alarms.
So, try switching briefly to setRepeating(). If it now works as you expect, your behavior is due to the "inexact" nature of setInexactRepeating().
Also, you can use adb shell dumpsys alarm to examine the scheduled alarms. It may be that you have two alarms scheduled, each going off once per hour.
setInexactRepeating() is the reason why it is not working as you expected. Try following:
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, firstStart, interval, pendingIntent );
In my application I have the functionality to trigger an alarm in 4 scenarios:
Only once for a user-chosen date and time
Daily for the chosen time
Weekly according to chosen date and time
User chosen custom days of the week
I successfully implement the first 3 scenarios by using the following:
Only once:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(date[0]));
calendar.set(Calendar.MONTH, (Integer.parseInt(date[1])) - 1);
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2]));
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
For daily scheduling:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
For weekly scheduling (as per system date):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
calendar.set(Calendar.SECOND, 0);
//long interval = calendar.getTimeInMillis() + 604800000L;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
How do I implement weekly alarm scheduling for custom days of the week?
private void scheduleAlarm(int dayOfWeek) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
// Check we aren't setting it in the past which would trigger it to fire instantly
if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 7);
}
// Set this to whatever you were planning to do at the given time
PendingIntent yourIntent;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, yourIntent);
}
private void setUpAlarms() {
scheduleAlarm(Calendar.MONDAY);
scheduleAlarm(Calendar.FRIDAY);
}
Attribution
Source: https://microeducate.tech/repeating-alarm-for-specific-days-of-week-android/ ,
Question Author : Dhruvil Patel(https://stackoverflow.com/users/1367157/dhruvil-patel) , Answer Author : MBH(https://stackoverflow.com/users/2296787/mbh)

How to set an alarm to fire properly at fixed time?

I have this code
Calendar c = new GregorianCalendar();
c.add(Calendar.DAY_OF_YEAR, 1);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 22);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 30*1000;
long a=c.getTimeInMillis();
// Schedule the alarm!
AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
c.getTimeInMillis(), 1*60*60*1000, sender);
It is not executed at 23:22h
What I am doing wrong? I noticed firstTime and c.getTimeInMillis() differs a lot in size and length. When I use firstTime, so when set to 30 seconds, the alarm is executed well.
You are using the AlarmManager.ELAPSED_REALTIME_WAKEUP flag, but you are using a Calendar object. These two things don't go together.
You need to use AlarmManager.RTC or AlarmManager.RTC_WAKEUP if you are specifying the alarm time using a Calendar or Date object (milliseconds since 1970).
You use AlarmManager.ELAPSED_REALTIME or AlarmManager.ELAPSED_REALTIME_WAKEUP when you are specifying the alarm time via SystemClock.elapsedRealtime() (milliseconds since the phone booted).
I had success with the following code, if you only want to set the alarm for the next occurance of hh:mm
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 22);
cal.set(Calendar.MINUTE, 19);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
//check if we want to wake up tomorrow
if (System.currentTimeMillis() > cal.getTimeInMillis()){
cal.setTimeInMillis(cal.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
}
To get the alarm to go off 30 seconds from now, use
Calendar cal = Calendar.getInstance();
to get the current time, and then
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+30000, sender);
Edit:
I think the problem is the ELAPSED_REALTIME_WAKEUP. This tells the AlarmManager that the time you are giving it is based on time since system startup. This is fine for 30 seconds from now, but if you want it to be based on real time you should use RTC, or RTC_WAKEUP. See javadoc for full explanation of those types.

Categories

Resources