How to set Multiple daily alarm? - android

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

Related

Battery Drain or Memory Issue

I am trying to create an App.. I used Alarm manager to run in every 1 minute so that it could run a AlarmReciever and can do a network task and Updating UI from Background.. Alarm Manager is firing in Activity.. I Just want to know will it affect battery by doing so? Also Like to know whether everytime i opened the activity will the AlarmManager will trigger. If so how can i tackle that situation..
I searched Everywhere most of the solution is to do AlarmManager rather than service as it will trigger even if app is killed..
My AlarmManager Code:
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
AlarmManager alarmManager = (AlarmManager) this.context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this.context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(), 60 * 1000, pendingIntent);

How to change repeating interval of AlarmManager at runtime in Android

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.

AlarmManager keeps screen of my device on

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.

Stop a service at specific time

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);

Alarm Manager : How to Call Alarm Manager at particular time?

I wanted to know How I can set Alarm for a particular time. For example I want to set alarm for
morning 9am daily. I googled a lot but found only the way to set alarm for a given interval only. like after 2 hours or next day.
hope this code helps you
Calendar calendar = Calendar.getInstance();
//9 AM
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, YourClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
you should create BroadcastReceiver to receive intent.
read the documentation for further details
I googled a lot but found only the way to set alarm for a given interval only. like after 2 hours or next day.
The second parameter to setRepeating() on AlarmManager is when you want the alarm to go off first. Set that to be 9am tomorrow using a Calendar object, and use an RTC or RTC_WAKEUP alarm.

Categories

Resources