I'm developing an application that can send a SMS in specified time, I have add some function on it, but i get stumped while i add function that can send a sms in every month on 14th, how can i make that function?
i have try the answer code on this link but didn't work.
I presume the problem is on the interval parameter in setRepeating function of AlarmManager class
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), interval , pendingIntent);
what the proper value of variable interval?
here interval is time in milliseconds between two alarms.
//e.g
long interval=5*60*1000;
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), interval , pendingIntent);
then my alarm will repeat every after 5 mins.
EDIT
int days=GetTotalDays(current_month);
interval=(days)*24*60*60*1000;
public int GetTotalDays(int current_month)
{
//here u can fetch current months total days
//suppose current month is 6(means july as it starts from 0)
//& u want to set alarm to next month(august)
//so get remaining days from calender of current month + day of next month
//e.g(14-7 to 14-8) so
//remaining days from calender of current month = 18(14-7 to 31-7)
//day of next month =14.
//so return would be (18+14-2=30).(-2.as it takes currentdate and nextdate also in calculation)
int currentdate=14;
int nextdate=14;
int totalDays=getDaysInMonthInPresentYear(6);
int myDays=(totalDays-currentdate)+nextdate;
return myDays-2;
}
public static int getDaysInMonthInPresentYear(int monthNumber)
{
int days=0;
if(monthNumber>=0 && monthNumber<12){
try
{
Calendar calendar = Calendar.getInstance();
int date = 1;
int year = calendar.get(Calendar.YEAR);
calendar.set(year, monthNumber, date);
days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
} catch (Exception e)
{
if(e!=null)
e.printStackTrace();
}
}
return days;
}
alarmManager .setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar1.getTimeInMillis() + AlarmManager.INTERVAL_DAY*30,
AlarmManager.INTERVAL_DAY*30, pendingIntent);
You can try like this
Related
I want an notification to appear each year depending on the date entered (Birthday). I have everything else working bar how to set a notification annually. As you can see below I have changed the code to say "HERE" where the intervals go. There are intervals for days and I know I could multiply that by 365. But what happens if its a leap year..
int REQUEST_CODE = 7;
Intent intent = new Intent(Activity2.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity2.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, System.currentTimeMillis(), HERE, pendingIntent);
You could replace 'HERE' with a method that determines if the following February from today is in a leap year, and then returns the value 365 or 366 days (in the form of milliseconds mind you) based on those checks.
private long millisUntilNextYear(){
//Set days in a year for Leap and Regular
final int daysInLeapYear = 366;
final int daysInYear = 365;
//Get calendar instance
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
//Get this year and next year
int thisYear = cal.get(GregorianCalendar.YEAR);
int nextYear = thisYear + 1;
//Get today's month
int thisMonth = cal.get(GregorianCalendar.MONTH);
//Get today's date
int dayOfMonth = cal.get(GregorianCalendar.DAY_OF_MONTH);
//Is today before February? If so then the following February is in THIS year
if (thisMonth < GregorianCalendar.FEBRUARY){
//Check if THIS year is leapYear, and return correct days (converted to millis)
return cal.isLeapYear(thisYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
}
//Is today after February? If so then the following February is NEXT year
else if (thisMonth > GregorianCalendar.FEBRUARY) {
//Check if NEXT year is leapYear, and return correct days (converted to millis)
return cal.isLeapYear(nextYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
}
//Then today must be February.
else {
//Special case: today is February 29
if (dayOfMonth == 29){
return daysToMillis(daysInYear);
} else {
//Check if THIS year is leapYear, and return correct days (converted to millis)
return cal.isLeapYear(thisYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
}
}
}
1) save dates in MM/DD/YY formats.
2) read these dates when you open your app(or at different times)
3) set alerts for single day/today it self.
Plus you can also show birthdays coming in next week/month etc.
In my Alarm application i have the following function for calculating the trigger time for the alarm , and its working perfect if the alarm time > current time , but if the alarm time is passed " Today Set time passed" current time > alarm time the alarm should fires tomorrow but in my function it fires in the same day after a few hours or minutes based on the difference amount . How can i fix this problem? what is the condition i should add to my function to wait 24 hours to tomorrow if the alarm set time is passed ? please help me
private long getTriggerTime(int hour,int minute)
{
long triggerTime = System.currentTimeMillis() ;
Calendar calendarNow = Calendar.getInstance();
int currentHour = calendarNow.get(Calendar.HOUR_OF_DAY);
int currentMinute = calendarNow.get(Calendar.MINUTE);
int HourDifference = currentHour-hour;
int MinuteDifference = currentMinute -minute;
if(HourDifference < 0 ){ HourDifference = HourDifference*(-1); }
if(MinuteDifference < 0){ MinuteDifference = MinuteDifference*(-1); }
triggerTime = triggerTime + ( HourDifference*60*60*1000) + (MinuteDifference*60*1000 );
return triggerTime;
}
Here how i use this function
am.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerTime(hour,min),AlarmManager.INTERVAL_DAY ,sender);
Note : hour , min in 24hour format
here is the sample code for alarm to fire if the alarm time > current time ,else it will for 24 hours.....
private long getAlarmTimeInMillis(int hour,int minute){
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);//
calendar.set(Calendar.SECOND, 0);
if (calendar.getTimeInMillis() < System
.currentTimeMillis()) {
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+ 1);
// add 24 hours to the calender.getTimeMillis
}
return calendar.getTimeInMillis();
}
try this.....
Use this:
Calendar calendarNow = Calendar.getInstance();
calendarNow.set(calendarNow.get(Calendar.YEAR), calendarNow.get(Calendar.MONTH),
calendarNow.get(Calendar.DAY_OF_MONTH),
calendarNow.get(Calendar.HOUR_OF_DAY),
calendarNow.get(Calendar.MINUTE), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendarNow.getTimeInMillis(),
24 * 60 * 60 * 1000, pendingIntent);
As the title implies, I'm looking to schedule a task to run on certain days at certain times. For example, I might have it run at 5:00 every Tuesday and Thursday. I've seen several scheduling methods for Android, but all of them seem to operate in the form of "do task after n delay" or "do task every n seconds".
Now I could probably jury-rig it by having it calculate the time to the next execution during the execution of the task itself, but that seems inelegant. Is there some better way to do this?
You've to set an Alarm to perform those tasks. Most probably you will end up calling a Service once the alarm is triggered:
private void setAlarmToCheckUpdates() {
Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY)<22){
calendar.set(Calendar.HOUR_OF_DAY, 22);
} else {
calendar.add(Calendar.DAY_OF_YEAR, 1);//tomorrow
calendar.set(Calendar.HOUR_OF_DAY, 22); //22.00
}
Intent myIntent = new Intent(this.getApplicationContext(), ReceiverCheckUpdates.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)this.getApplicationContext().getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
However, if you need to set specifically a day:
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (weekday!=Calendar.THURSDAY){//if we're not in thursday
//we calculate how many days till thursday
//days = The limit of the week (its saturday) minus the actual day of the week, plus how many days till desired day (5: sunday, mon, tue, wed, thur). Modulus of it.
int days = (Calendar.SATURDAY - weekday + 5) % 7;
calendar.add(Calendar.DAY_OF_YEAR, days);
}
//now we just set hour to 22.00 and done.
Above code is a little bit tricky and mathematic. If you wan't something stupid aswell as easy:
//dayOfWeekToSet is a constant from the Calendar class
//c is the calendar instance
public static void SetToNextDayOfWeek(int dayOfWeekToSet, Calendar c){
int currentDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
//add 1 day to the current day until we get to the day we want
while(currentDayOfWeek != dayOfWeekToSet){
c.add(Calendar.DAY_OF_WEEK, 1);
currentDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
}
}
How do I get 11:59 PM of the previous night and 12:00 AM of tonight expressed as long values???
So many different date options I'm confused
you can use this calculation to do so.
//11:59 PM of the previous night and 12:00 AM of tonight
Calendar prevDate = Calendar.getInstance();
if(prevDate.get(Calendar.DATE)==1)
{
if(prevDate.get(Calendar.MONTH)==0)
{
prevDate.set(Calendar.YEAR, prevDate.get(Calendar.YEAR)-1);
prevDate.set(Calendar.MONTH,Calendar.DECEMBER);
prevDate.set(Calendar.DAY_OF_MONTH,31);
}
else
{
prevDate.set(Calendar.MONTH,prevDate.get(Calendar.MONTH)-1);
prevDate.set(Calendar.DAY_OF_MONTH, prevDate.getMaximum(Calendar.DAY_OF_MONTH));
}
}
else
{
prevDate.set(Calendar.DAY_OF_MONTH,prevDate.get(Calendar.DAY_OF_MONTH)-1);
}
prevDate.set(Calendar.HOUR_OF_DAY, 11);
prevDate.set(Calendar.MINUTE, 59);
Calendar currDate = Calendar.getInstance();
currDate.set(Calendar.HOUR_OF_DAY, 0);
currDate.set(Calendar.MINUTE, 0);
long prevDateLong = prevDate.getTimeInMillis();
long currDateLong = currDate.getTimeInMillis();
Log.i("", prevDate.toString()+" >>>>>>>> "+currDate.toString());
Log.i("", prevDateLong+" >>>>>>>>> "+currDateLong);
All Java dates are longs underneath. It's the number of milliseconds since midnight on 1-Jan-1970.
Date today = new SimpleDateFormat("yyyy-MMM-dd").parse("2012-Oct-28"); // today at midnight
long millis = today.getTime();
I'd recommend that you look at the java.util.Calendar class. It's not hard, just tedious.
http://www.exampledepot.com/search/luceneapi_node/Calendar
How can I get Time in Millisecond from Calendar Object in order to set an interval in Alarm Manager I am using the following code:
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, sender);
and my calculation for interval for getting One month of MilliSeconds is as follows:
Calendar monthCal = Calendar.getInstance();
monthCal.clear();
monthCal.setTimeInMillis(System.currentTimeMillis());
monthCal.add(Calendar.MONTH, 1);
interval = monthCal.getTimeInMillis();
but I am not getting any repeating Alarms.
You can do the following:
Calendar c = Calendar.getInstance();
c.set(year,month,1); // Set the year and month you want milliseconds for
long daysInMonth = c.getActualMaximum(Calendar.DATE); //Get how many days this month have
long millisInMonth = daysInMonth*dayMillis; //Multiply the amount of days with how many milliseconds a day consists of (86400000L)