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

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.

Related

How to get my service started periodically at specific time of the day

I created a service that will check some online data in specific time of the day and everyday (for example at 6 AM), and rather than using a background running service and broadcast receiver I decided to use AlarmMananger and the code below:
Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
My questions are:
1- How to unregister from the AlarmManager when user decided to de-activate my service?
2- What is the use of this line
calendar.setTimeInMillis(System.currentTimeMillis());
3- What could happen if I called the same function above with the same params and data, will AlarmMananger overwrite my request or it will register a new one, and when the time comes (6 AM) it will call my service twice?
4- Is it possible to check AlarmMananger if it has successfully registered my Intent?
Edit 1:
The code below is not triggering my broadcast receiver, why?
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6); // For 6 AM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, mybroadcastreceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
AndroidManifest.xml
<receiver
android:name=".mybroadcastreceiver"
android:enabled="true"
android:exported="true"></receiver>
Coming to all your questions :-
How to unregister from the AlarmManager when user decided to de-activate my service?
Answer :- To cancel an AlarmService you created, you need to cancel it like this.
this.getAlarmManager().cancel(pendingIntent);
where this is the service (or Activity/Context) from where you called the AlarmService.
What is the use of this line
calendar.setTimeInMillis(System.currentTimeMillis());
Well if you are aware of the method Date.setTime(), which generates the Date object with milliseconds provided in the argument. Calender.setTimeInMillis() does the same for the Calender object. System.currentTimeMillis() is used to provide the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. ( I guess you are aware of whole Timestamp concept)
3. What could happen if I called the same function above with the same params and data, will AlarmMananger overwrite my request or it will register a new one, and when the time comes (6 AM) it will call my service twice?
As the documentation says, If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled.
4. Is it possible to check AlarmMananger if it has successfully registered my Intent?
I think it is possible, but there is no such method to check it directly, you need to do the hack. I think you will find this answer suitable.
I hope this helps you :)
You can use if else function for register and unregister from AlarmManager.
for example checkBox isSelectted() or not.
calendar.setTimeInMillis(System.currentTimeMillis());
this line will return you system time in millisecond format. This format is like this "2547889955511"
yes if you can perfectly set up AlarmManager your pending intent can call your service twice.
yes you have to set up perfectly then it will possible to check AlarmMananger is it successfully registered your Intent or not.

Android - AlarmManager

I'm currently making a native Android app and I have code working to schedule a notification to appear on the device using the AlarmManager class:
Intent intent = new Intent(this, NotifyActivity.class);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, alarmIntent);
Two things:
The notification goes off every time the app is opened. I don't want this to happen.
The notifications is suppose to be going off at 9am device time (or so I'm led to believe). This is not happening and it looks like it goes off every 9 hours or so.
Can anyone tell me why the notification goes off every time the app is opened and why the notification is not only being triggered when the device reaches 9am?
Thanks
From the doc of AlarmManager:
If the stated trigger time is in the past, the alarm will be
triggered immediately
You are setting your alarm to 9:00 , if you launch after this hour, trigger time is in the past, so the alarm is going off when the app is opened.
You can check if the current time is after alarm, if so, you add one day to the alarm and it will going off the next day. For example:
Calendar cal = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
if (cal.before(cal_now)) {//if its in the past
cal.add(Calendar.DATE, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, alarmIntent);
Hope it helps,

android alarm manager triggers after time

I realize that this alarm triggers even after finish time in android is a similar question. However, mine's got a little bit different.
I wonder, for example, if i set time to be 14:40, and launch the app at 15:00 (or any other time after 14:40:00), the alarm manager's onReceive method will be triggered immediately.
Actually, this is a confirmation rather than a question since my application seems to do this.
Thank you !
CODE
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 40);
c.set(Calendar.SECOND, 0);
//System.out.println();
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
Try with the AlarmManager.setExact()
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);

How to set Multiple daily alarm?

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

AlarmManager not setting, Alerting everytime activity is opened

I am trying to use this to set an alarm that goes off everyday.
String alarm = Context.ALARM_SERVICE;
Calendar calendar = Calendar.getInstance();
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
Intent intent = new Intent("NEW_ITEM");
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
calendar.setTimeInMillis(System.currentTimeMillis());
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1*AlarmManager.INTERVAL_DAY, sender);
Without running it, the code looks good to me... Obviously, if you set this alarm every time you start the activity, the alarm will go off immediately since: am.setRepeating(AlarmManager.RTC_WAKEUP, **calendar.getTimeInMillis()**, 1*AlarmManager.INTERVAL_DAY, sender); Tells the alarm manager to alert right now (2nd param) and to repeat in a day (3rd param, assuming your constant is correct).
If you want the alert to start only in 24 hours, simply change the line to:
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, sender);
Code looks good, but you have to be aware of one thing. If the user decides to set the alarm again (for example, by hitting the 'set the alarm' button) the old one will be replaced. If you want to avoid this, check out this topic: Using Alarmmanager to start a service at specific time

Categories

Resources