How to create Recurring of Alarm in Android After taking user input? - android

I am an android developer.I am making an application in which I have to take user input like
8:00,9:00,10:00 etc and I have to set alarm for 20 or 30 days for each time user enter .A user can enter two or three or any times the timings.I know there is a function
alarm.setRepeating(AlarmManager.RTC,System.currentTimeMillis()+timeinminutes*60*1000,30*1000, pintent);
But I have to set the alarm for ever or days entered by user .So can any body tell me .How can I do this .

the following code help you,firstWake the app will run at very fast time,you can specify as your requirements
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
long interval = android.text.format.DateUtils.DAY_IN_MILLIS*20;//or 30
long firstWake = System.currentTimeMillis() ;
am.setRepeating(AlarmManager.RTC,firstWake, interval, pendingIntent);

Maybe you can use another nonrepeating alarm for 20 or 30 days that stops repeating alarms.

Related

setExactAndAllowWhileIdle is almost triggered exactly

I searched the Internet for an answer but have not found anything.
I wrote an app where every 17 minute an SMS must be sent ( every 17 minute means: 10:17:00 and not at 10:17:49 )
Therefore, I use an AlarmManager with setExactAndAllowWhileIdle, and AlarmManager.RTC_WAKEUP.
Long whenToFireOff = treeMapInstance.getFirstEntryInTreeMap().getKey();
Calendar alarmCal = Calendar.getInstance();
alarmCal.set(Calendar.HOUR_OF_DAY, treeMapInstance.convertMillisecondsToHour(whenToFireOff));
alarmCal.set(Calendar.MINUTE, treeMapInstance.convertMillisecondsToMinutes(whenToFireOff));
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmCal.getTimeInMillis(), pi);
However, it gets always triggered round about 10:17:49, and the next scheduled alarm also at 10:34:49, and so on.
I calculated my next alarm correctly. I am absolutely 100% sure :)
Is setExactAndAllowWhileIdle not as exactly as it is written in the documentation?
Thanks

A repeating alarm doesn't work if the system time was changed to the past

I am new to android and have problems with setting a repeating alarm. The app is like this: user selects time using a timepicker, and selects several days to set the alarm. I use this code to implement this task:
for(int i=0; i < alarm.getDaysOfTheWeek().size(); i++){
// here is a switch block to assign daysUntilAlarm1
//to a particular day from the list of selected days, like
//daysUntilAlarm1 = Calendar.SUNDAY;
calendar.set(Calendar.DAY_OF_WEEK, daysUntilAlarm1);
calendar.set(Calendar.HOUR_OF_DAY, alarm.getHour()); // hour picked by user
calendar.set(Calendar.MINUTE, alarm.getMinute());
pendingIntent = PendingIntent.getBroadcast(AlarmManagerActivity.this, i, intentToAlarmReceiver, 0);
pendingIntents.add(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
The problem is when I test alarms, I change the system time to the next alarm day. Everything works fine. But when I change the system time to the past, like last week, the alarm doesn't work. So if user will have to change the time of the device to past, the app will not work. Can anyone help me, please?

How to get location updates in certain time interval only?

In my android application I want to get location updates only for certain time interval say like from morning 9 to evening 9.
How is that doable. Please share your thoughts on it.
Thanks in Advance.!
This is my solution to this problem.
use it in a running background service or something like this:
Calendar c = Calendar.getInstance();
int hofday = c.get(Calendar.HOUR_OF_DAY); //getting the current hour of day
if (hofday<21 && hofday>9){ //between 9am. and 9pm.
//start collecting gps data
}
else{
//it is not between 9am. and 9pm.
}
You set an alarm for 9 am and 9 pm. Then in your broadcast receiver for the first you turn on location updates, and for the second you turn it off.
This is a good tutorial on how to schedule repeating alarms. For location updates I recommend using LocationListener.

Why alarmmanager not exactly repeats an event android

Please help me. Why does AlarmManager not exactly repeats an event when the repetition period of more than one day?
Here is my code to run AlarmManager. The variable time_period contains the following value 60*1000*60*24*7 which is equal to 7 days. In the end, if I change the date on your phone, the event is triggered by 6-4-7-7-7-7 days.
This can be seen in the logs of the application (See link http://prntscr.com/7kdqbw ) Thanks in advance for your reply.
Intent notification = new Intent(this , ServiceReminders.class);
notification.putExtra("backup", "backup");
AlarmManager alarmManagerBackup = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pibackup = PendingIntent.getService(this, 3, notification, 10);
if (sdkVersion < 19) {
alarmManagerBackup.setRepeating(AlarmManager.RTC_WAKEUP, 0, time_period, pibackup);
}
else if (sdkVersion >= 19) {
alarmManagerBackup.setInexactRepeating(AlarmManager.RTC_WAKEUP, 0, time_period, pibackup);
}
Log.d("ServiceManagerNotification", "AlarmBackup sdkVersion = "+sdkVersion);
You are explicitly leveraging the inexact alarms by calling setInexactRepeating(). Unsurprisingly, this results in your alarms being set at inexact intervals.
The problem is further compounded by the fact that you are supplying a custom period. If you do not use one of the pre-defined intervals, then the framework will simply call setRepeating() using your interval instead of using setInexactRepeating().
From the documentation for the intervalMillis parameter:
interval in milliseconds between subsequent repeats of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY then the alarm will be phase-aligned with other alarms to reduce the number of wakeups. Otherwise, the alarm will be set as though the application had called setRepeating(int, long, long, PendingIntent). As of API 19, all repeating alarms will be inexact and subject to batching with other alarms regardless of their stated repeat interval.
In the end, regardless of how you set this alarm, it will be inexact if you set it as a repeating alarm and there isn't a whole lot you can do about it.
If you need more precision, you should use either setWindow() or setExact() and set the next alarm every time your alarm triggers. Although if you are only performing an action once a week, it is likely that in the end you don't need that precision.

AlarmManager.RTC_WAKEUP not working for some devices

I am using AlarmManager in my application to set alarm for a particular time. I have used AlarmManager.RTC_WAKEUP to set the alarm. When I am testing the same it's working on number of device like Lg optimus, Sony Xperia etc. But while testing the same app in Samsung Galaxy S3 I found that alarm is not working. I am still unable to understand why is this happening.
I am using following code to set alarm :-
// create the object
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,cal1.getTimeInMillis(), PendingIntent.getBroadcast(getActivity(),reminderId, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Someone please help me to solve this strange problem. Any help would be appreciable.
Thank you
I'm not sure but it sounds the problem is with the way you set cal1's time and maybe different timezone.
I'm not sure what the particular issue you are having is, but you could try to replace cal1.getTimeinMillis() with System.currentTimeMillis() + your time delta in ms. This works consistently for me.
Is your testing getting tripped up by the long-time Android OS bug https://code.google.com/p/android/issues/detail?id=2880 ? If the clock gets set backwards, Intent.ACTION_DATE_CHANGED broadcast won't be delivered until the clock catches up to what was going to be the alarm time. (Reboot to reset this broken state.) That's because this Intent is based on an AlarmManager alarm which does not get updated when the clock changes.
That situation is pretty rare in normal use. But it occurs frequently when testing alarm code.
If you're testing an AlarmManager alarm by adjusting the clock, AFAIK any alarm set to a target Calendar on any Android device will get messed up by adjusting the clock. You can work around it by rescheduling your alarm whenever the clock gets adjusted. This assumes the system properly broadcasts Intent.ACTION_TIME_CHANGED so you can tell when the clock gets adjusted. (That Intent is misnamed. It's not like Intent.ACTION_DATE_CHANGED which occurs when the date changes due to time passing.)
I think you have problem in your "cal1" object;
When you set time in calendar object, actually setting Month, you should not use exact numeric value of month;
ex: - April = 4, you must use 3 instead of 4
I face this problem in API Level 19, but don't know about others.
This Question may give you better explanation.Why month give wrong value
public Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,2014);
c.set(Calendar.MONTH, 03);//One minus from actual month numeric value
c.set(Calendar.DATE, 24);
c.set(Calendar.HOUR_OF_DAY,16 );
c.set(Calendar.MINUTE, 39);
c.set(Calendar.SECOND, 0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);

Categories

Resources