android : setting alarm on monthly basis - java

I am developing an android app for setting the alarm on monthly basis. I have given the below code for next month.
private int GetTotalDays(int monthRecvd1) {
int totalDays=getDaysInMonthInPresentYear(taskMonth1);
myDays=(totalDays-taskdate1)+taskdate1;
}
private int getDaysInMonthInPresentYear(int taskMonth1)
{
int days=0;
alarmCalendar.set(Calendar.HOUR, Hrs);
alarmCalendar.set(Calendar.MINUTE, Mins);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.DATE, date);
alarmCalendar.set(Calendar.MONTH, Month);
alarmCalendar.set(Calendar.YEAR, year);
Long alarmTime = alarmCalendar.getTimeInMillis();
calendar.set(year, Month, date);
days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
This code works only for next month. and doesn't work on second next month. for example, if i set the alarm on april, it will set for may but not for june and so on.
Can anybody tell me what could be the solution for this.
Please help! Thanks!

Use setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) method rather than just AlarmManager.set(). You can find more info on documentation.
http://developer.android.com/reference/android/app/AlarmManager.html
Edit: It should like this
public void onClick(View arg0) {
Intent serviceIntent = new Intent(AndroidAlarmService.this, this);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, serviceIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long now = calendar.getTimeInMillis();
calendar.add(Calendar.DATE,calendar.getActualMaximum(Calendar.MONTH));
long repeat = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, now , repeat , pendingIntent);
}

try this..
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, intent);

The solution is to explicitly force the literals to be of type long, which is a 64-bit number:
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTimefor30, 30L*1440L*60000L , pi);
Source:
Setting alarm on monthly basis android

Related

Android Notification triggering on application startup

I'm trying to get my Daily Notification Service working properly but I am stuck with an issue.
I should receive a notification only at a specific time of the day in the future but I noticed that
if (time_that_i_set_for_notification < current_time_of_the_day) notification triggers at the boot of my app
That's wrong because in that condition, the notification should trigger only the next day, not in the instant I launch the app.
Here's my attempt to get the things working:
I call this in my MainActivity, onCreate() method:
private void scheduleNotification(int hour, int minute){
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
notificationIntent.putExtra("notifId", notifId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0);
// Set the alarm to start at approximately at a time
DatePicker datePicker = new DatePicker(this);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 12);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
I thought that adding if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); I would've achieved the result I needed.
Thank you and have a nice day.
Before calling setInexactRepeating, add this check:
if (calendar.getTimeInMillis() < System.currentTimeMillis())
calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY);
Hope this helps.
Ok there's a wrong check there, if you change it, you should be able to solve your issue.
Here we go:
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
notificationIntent.putExtra("notifId", notifId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0);
// Set the alarm to start at approximately at a time
DatePicker datePicker = new DatePicker(this);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
//if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 50);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
I commented //if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);
because it should be unnecessary.
Let me know if it worked.
UPDATE: You should totally check whether the Service that handles your daily notification is high consume or not. A High-draining battery app would bother your users.

Set repeating alarm

i want to set alarm to repeat it in a particular day .for example every monday at 4:20 ,
I have created a method but not working .please help me to solve this problem
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.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 *7, pendingIntent);
}
now if i choose monday 2:40 its only alarming on next monday 2:40 not working on every monday 2:40
You can try use setInexactRepeating instead.As of API 19, all repeating alarms are inexact.You can refer to the official documents in detail.
Use setInexactRepeating function it worked for me.

Issues scheduling alarms when getting the time to set the AlarmManger for using a TimePicker because of AM/PM

I'm programming an app that has a feature that the user can use to set an alarm for an SMS message to be send to someone. The problem I am having is say the user sets the Alarm-Manager to go off at 9:00 A.M. and it is currently 10:00 P.M. then the alarm immediately goes off. It doesn't recognize the fact that the alarm should be set for the next day.
Here is the code for setting up the AlarmManager:
Intent intentAlarm = new Intent(MainMenu.this, AlarmReceiver.class);
intentAlarm.putExtra("phoneNumber", phoneNumber);
intentAlarm.putExtra("message", message);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(MainMenu.this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Other than the AM/PM issue, the AlarmManager works well. I just don't know where to go from here and all of the documentation I am looking at just doesn't help.
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 10); // use hour of day for am pm issue
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);
Calendar now = Calendar.getInstance();
if (now.after(cal1))
cal1.add(Calendar.DAY_OF_YEAR, 1);
// this code snippet add one day if the current time passed away
//else use cal1.set(year, month, day); for exact day
public void SetAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
// set alarm after 12 hours
long after = System.currentTimeMillis() + 12 * 60 * 60 * 1000;
am.set(AlarmManager.RTC_WAKEUP, after, pi);
}

Android AlarmManager: how to find out the time remaining to trigger the PendingIntent

I am developing an alarm clock app. Below I have a function to set the alarm. But I want to know how to find the remaining time AlarmManager to trigger the PendingIntent.
For example, it is now 10:00 am, and set the AlarmManager to trigger the PendingIntent 23:00, and the calculations we know that the PendingIntent will be called here 13 hours. But how to find out this time remaining?
Sorry for my poor english. And I thank you for your attention
String schedule = "23:00"; //example
Calendar cal = Calendar.getInstance();
cal.set(cal.HOUR_OF_DAY, getTime(schedule));
cal.set(cal.MINUTE, getMinute(schedule));
cal.set(cal.SECOND, 0);
cal.set(cal.MILLISECOND, 0);
DateFormat dfH = new SimpleDateFormat("HH");
DateFormat dfM = new SimpleDateFormat("mm");
int currentTime = Integer.parseInt(dfH.format(new Date()));
int currentMinute = Integer.parseInt(dfM.format(new Date()));
Intent i = new Intent(context, RecebAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context.getApplicationContext(), id, i, 0);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long totalTime = cal.getTimeInMillis();
if (currentTime > getTime(schedule) || (currentTime == getTime(schedule) && currentMinute >= getMinute(schedule))) {
alarms.set(AlarmManager.RTC_WAKEUP, totalTime + AlarmManager.INTERVAL_DAY, pi);
} else {
alarms.set(AlarmManager.RTC_WAKEUP, totalTime, pi);
}
You can calculate the difference between your alarm time (totalTime) and the current time like this:
long timeDiffInMillis = totalTime - System.currentTimeMillis();
This will give you the time remaining before your alarm will trigger (in milliseconds).

Android - Trying to set an alarm on the first Monday of each month

I am trying to create an alarm on the first Monday of each month. Here is my code:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, FilterOrderBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_MONTH, 1);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
if(Calendar.getInstance().getTimeInMillis() - calendar.getTimeInMillis() > 0 ) {
calendar.add(Calendar.MONTH, 1);
}
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() - calendar.getTimeInMillis(), pi);
Today is the 12th of June. This code creates an alarm trigger that is 1382399999 millis away or 15.9 days. That will only take me to the end of the 27th. I have thought about doing it with Joda, but there are issues with the Joda library with Android. It is really slow. Can anyone show me how to do this with the native Android libraries? Thanks for the help.

Categories