Battery Drain or Memory Issue - android

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

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.

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

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

Categories

Resources