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.
Related
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?
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);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 1);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context,MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7*24*60*60*1000, pi);
I have this simple pending repeating alarm and it works just fine only when system android alarm is not set up. It doesnt matter on what time alarms are set in system Alarm app, looks like this system app stops all my pending intents. Any idea how to debug this? What can cause this problem ?
I would like to use it in paralel with system alarms.
make sure you have permission
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
For Debugging:
override onNewIntent() in your activity MyClass Activity like this:
#Override
onNewIntent(Intent intent){
android.os.Debug.waitForDebugger();
if(intent.hasExtra("alarm")){ //Put a break point here
Log.d("Alarm fired","yes");
}
}
Create you PendingIntent for firing alarm like this:
Intent intent=new Intent(context,MyClass.class);
intent.putExtra("alarm","yes");
PendingIntent pi = PendingIntent.getService(context, 0, intent,0);
So, when your alarm gets fired, control will come to onNewIntent and wait for you to attach a debugger. That time go to Attach debugger button in android studio and attach debugger to your application. And this is how you will know that you alarm has been fired.
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.
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