alarm is not triggering after 5,6 days - android

I am installing an alarm in my app which triggers every day at defined time and does some execution. My alarm gets trigger fine everyday but after 5 to 6 days it stops triggering. I did not reboot my device. What should be the reason behind it?
here is my manifest
<receiver android:name=".Auto_Slot_forwarding.AlarmReceiver" />
and the code where i am setting my alarm with specific time everyday
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent intent1 = new Intent(AutoForwarding.this, AlarmReceiver.class);
intent1.putExtra("requestCode", code);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AutoForwarding.this, code, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) AutoForwarding.this.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+(days * AlarmManager.INTERVAL_DAY), days * AlarmManager.INTERVAL_DAY, pendingIntent);
Any idea what i am doing wrong?

Related

Alarm is not fired after memory is cleared

I am writing some code to fire alarm at a specific time. Everything is working fine and alarm is fired as expected.
But when I clear memory using some Memory Cleaner app, then the alarm is not fired.
I have added permissions and intent-filter for Boot Completion that set my alarm after device boot. But I don't know how to reset my alarm if any Memory Cleaner app cleans the memory?
Code for setting alarm
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
Intent intent = new Intent(context, DailyAlarmService.class);
PendingIntent pIntent = PendingIntent.getService(context, Constants.DAILY_ALARM_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pIntent);
Thanks in Advance.

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,

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.

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

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