I have service run in background.
I want to call this service at specific interval.
if user provide values 10, 20, 30 than service should call after 10 min,20 min and 30 min receptively.
How can I do above thing?
AlarmManager will help you :)
It allows you to set up a schedule to launch your application's components during the specified time range
Update
To instantiate an AlarmManager that will go into play at the specific period after it's instantiating, configure it with setRepeating() method and PERIOD parameter added to SystemClock.elapsedRealime():
AlarmManager mgr = (AlarmManager)getSystemService(ALARM_SERVICE);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi); // Here PERIOD is a value specified by you as PendingIntent object
You can set an Alarm to have an Intent fired at specific interval. You can also set an inexact alarm if it is not critical that the alarm runs at the precise time.
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 10);
startTime.set(Calendar.MINUTE, 20);
startTime.set(Calendar.SECOND, 25);
Intent dailyQuotes = new Intent(getActivity(), DailyQuotesService.class);
AlarmManager am = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(getActivity(), 0, dailyQuotes, 0);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, startTime.getTimeInMillis(), 30000, pi);
here is my code and it call my service exactly on specified time. and then is repeated every 30 secs.
Related
I'm new to android and using alarmManager and I was wondering if there is a way to set an alarm in android that triggers for example every monday until a certain specific date. Like this :
Start date 10/09/15
Remind me something every monday at 2:30 pm
Until
End date 11/09/15
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 30);
int weekInMillis = 7 * 24 * 60 * 60 * 1000;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
weekInMillis, PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT));
Above code snippet sets an alarm for 2:30 PM that repeats itself every week. Tweak calendar for varying the time at which the alarm goes off. For example, the coming Monday.
When the alarm goes off, it sends a broadcast which will be received by ReminderWakefulBroadcastReceiver, a custom receiver containing the code that you want to run every Monday at 2:30 PM. This code should also check whether it is time to cancel the alarm and if it is, the following code cancels it:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderAlarmWakefulBroadcastReceiver.class));
References:
AlarmManager, Scheduling Repeating Alarms, PendingIntent
If you know how to setup an Alarm, the solution is quite simple:
1) At the time you setup the Alarm, calculate the maximum timestamp you want it to run, and save it as a local preference.
2) Then in the Alarm code itself, each time it is triggered you can make a first test to see if the current timestamp is before or after your limit preference saved at first time.
3) If reached, then cancel the Alarm as #karthik said. If not, keep your code going...
I have written the following AlarmManager to start a Service which will download a file at specified interval:
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 7);
Intent downloader = new Intent(mActivity, FileDownloadService.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mActivity, 0, downloader, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
From the other part of the application, I get the value for repeating interval to run the Service. The value for repeating interval change on daily basis.
How can we change the AlarmManager interval at runtime as the above code will be executed only once?
For example, on day 1 I want the repeating interval to be 15 minutes and on day 2 I want the interval to be 2 hours.
How can I change the repeating interval dynamically?
Store the value 15 minutes inside your pending intent as an extra. When your code gets triggered by the alarm manager, get the current value, increment it as per your needs and reset the extra in the intent which holds the value.
Edit : Use alarmManager.set(int type, long triggerAtMillis, PendingIntent operation); at every new invocation of your code.
I have one problem I need to set AlarmReceiver.
I am using this code for it:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 5);
AlarmManager alarm = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(getActivity(), 0, i, 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*5, pIntent);
So it means that this AlarmManager will call AlarmReceiver every 5 seconds, but problem is that if I don't turn my screen OFF manually, screen will stay ON and this is not what I want.
From the AlarmManager reference documents:
The parameter AlarmManager.RTC_WAKEUP, will wake up the device (in case of device sleep) to deliver the Alarm. You may use AlarmManager.RTC but that won't be wake up the device and your Alarm won't be delivered until next time device wakes up.
A better option would be to use a Service for this purpose, as they are designed to carry out the background tasks.
I am starting my service using below code repeatedly. My service starts at 8am everyday. And AlarmManager repeates at every 1 min. I want to stop this sevice at 6pm. how can I do this ?
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent loggerIntent = PendingIntent.getBroadcast(this, 0,new Intent(this,AlarmReceiver.class), 0);
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 08);
timeOff9.set(Calendar.MINUTE, 00);
timeOff9.set(Calendar.SECOND, 00);
//--------------------------------------------------------------------------------------------------------------------
long duration = userinterval * 60 * 1000;
manager.setRepeating(AlarmManager.RTC_WAKEUP,timeOff9.getTimeInMillis(), duration, loggerIntent);
In order to cancel at 6pm exactly, I would consider 2 options:
Each time the alarm triggers (i.e. every 1 minute), check the time, and cancel if time is after 6PM.
Set a once-off alarm in AlarmManager to go off at 6PM exactly. In that alarm, cancel.
I prefer option 2 for simplicity, and modularity of each code block. So for me, I would use (2) in my proof-of-concept code, while working on the solution.
But option 1 is better from a resources point of view, as the Android system only needs to remember a single alarm. I would use (1) in my final production code.
This way of working is just my personal preference, and most ppl probably will say to use (1) right away from the start.
The details about cancelling are below...
As for how to cancel an alarm, you don't often beat an answer by #commonsware....
Below answer copied from How to cancel this repeating alarm?
Call cancel() on AlarmManager with an equivalent PendingIntent to the one you used with setRepeating():
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this,
0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
I want to set more than one daily alarm in my android application for that I am making demo code like this
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_ONE_SHOT);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 30000, pi);
I has given 30000 ms as a interval, so I think it should be repeat every 30 second. but not repeating. its ringing once after 1 min from I started the app is I am wrong ? and what should I do to set multiple daily alarm in My application ?
Thanks!
I think your problem lies in your PendingIntent with the flag FLAG_ONE_SHOT, so with this you can only set your alarm once. If you want repeating alarm, try using the flag FLAG_UPDATE_CURRENT.
Source: http://developer.android.com/reference/android/app/PendingIntent.html