AlarmManager keeps screen of my device on - android

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.

Related

not able to set alarm for more than 20 seconds

I had created alarm manager which invoke after 2 minutes which I want. But currently, i'm not able to set it more than 20 seconds. if I set for 20 seconds it returns output but if I set it for more than 20 I'm not able to set. I want to set it for 2 minutes. kindly help me.
I had added alarm fire code
AlarmManager manager = (AlarmManager) ContextGetter.getContext().getSystemService(Context.ALARM_SERVICE);
Intent intnt = new Intent(ContextGetter.getContext(), AlarmBroadcastReceiver.class);
intnt.setAction("com.demo.alarmEvent");
PendingIntent pending = PendingIntent.getBroadcast(ContextGetter.getContext(), 0, intnt, 0);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 30*1000, pending);
kindly help me
i want it set it only once.
I also tried with this one.
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intnt = new Intent(EnterSysNumber.getInstance(), AlarmReceiver.class);
intnt.setAction("com.demo.Enter_number");
PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending);
In Android N you'll run into Doze mode. Doze is a deep sleep mode to save battery power. It will prevent your service to preserve battery power.
AlarmManager and using setExactAndAllowWhileIdle when in low-power idle modes this duration may be significantly longer, such as 15 minutes.. But this is NOT suggested for any Play Store app, you will kill your user's battery.

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.

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

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

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