AlarmManager triggers when phone is locked - android

I am setting an alarm with AlarmManager to trigger every hour. I don't need to wake up the device, so i'am not using the WAKEUP type of alarm.
But i was wondering what happens when the phone is locked for many hours and then, the user unlocks the phone.
Does the AlarmManager stacks all the alarms triggered while the phone was locked and send all together when the phone is unlocked?
If yes, is there any way to prevent this and only recive the last one?
Thanks.

Related

Booting Android Device on AlarmManager Call

I am building an Android Alarm Applicaion, and used following code for Main Calling Class:
AlarmManager inst_alarm= (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent =new Intent(MainActivity.this,Alarm.class);
pintent= PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
inst_alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pintent);
//"cal" is reference of calendar class to get saved time in millisecond.
for Service Class:
public class Alarm extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context c, Intent i) {
Uri uri_alarm= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(uri_alarm==null)
{ uri_alarm=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone_alarm=RingtoneManager.getRingtone(c, uri_alarm);
ringtone_alarm.play();
}
}
This code work fine but i have few quesions.
Question: 1) Is it possible to alarm bell on scheduled time if android device is switched off(device get power on automatically to ring bell on saved time) as in default application of android device does.
2) Suppose i saved alarm after 8 minutes exactly and i restart my device then alarm start ringing immediately after boot(Because i have used "bootcomplete" for receiver in manifest) but i want to play alarm after 8 minutes exactly not on reboot.(I have saved alarm in shared-preference but how to use it on reboot)
EDIT: Can I use "Power Manager" class or any other class to handle above mentioned situation ?
1) Is it possible...
No, not from complete power off. You're most likely referring to screen being off and the device in low power state. If the device is turned completely off then alarms cannot wake it. The default Android clock app just wakes it from sleep. If you'd like to wake up the screen and play sound, you'll have to create a Service which can be activated via your WakefulBroadcastReceiver and have it start the appropriate UI. If you do not, then the system can go right back to seep when your onReceive() is done.
2) ...want to play 8 minutes after...
What you're likely seeing is a previous expiration of your Alarm firing and you're waking up the device (not powering it on.) Alarms are not retained across power cycles or reboots. If you wish to have them stick around after a true reboot, then you'd have to manage that yourself. The PowerManager and AlarmManager do not provide such a facility.
For (1), if you're talking about complete shutdowns, it's unfortunately impossible. There is no way to do this without specialized hardware (and its corresponding software interface to your app).
For (2), you can do something clever. For example, persistently store the timestamp whenever the device restarts/turns off (you can register a BroadcastReceiver for ACTION_SHUTDOWN in addition to your BOOT_COMPLETE) when you have a pending alarm bell, and use that information on reboot/boot to resume (or stop, depending on the time difference and the alarm bell's "timer") or otherwise re-sync your pending alarm bell logic.
And for your PowerManager question, again if we're talking about complete shutdowns, unfortunately the answer is no.
PowerManager is used to mainly prevent the device from going into deep sleep. Remember, Android devices turn off their CPU's some time after they are locked, preventing your app from doing calculations/processes. This is essentially what the notion of "wakelocks" is about.

Access the device clock alarm settings?

My app needs to wake the user up in the night so I would like to access the device's clock alarm and add my own in there. The alarm times shifts throughout the month, so I would need to access it and update it.
For example, I would like to add a daily alarm in the device clock alarm. Then everyday, I would like to run some calculation and adjust the time for the daily alarm.
Is this possible? I found AlarmManager but this seems like a scheduler not the device clock alarm. Perhaps there is an intent for the clock alarm? The screenshot below is the setting I am trying to access from my app (without rooting the device).
Here you have a tutorial about programming alarms for Android.
https://developer.android.com/training/scheduling/alarms.html
The thing is that is not needed to access the native clock app to set them, you can do it with your own app.
Be aware that this will not program an "Alarm"(with music and so on), it will only provide you the possibility to shcedule some task. In your case this task could be wake up the user with a loud music or whatever.

Prevent alarm from going off when device is asleep

I've set a repeating alarm that I only want going off when the device is awake. When it's asleep, I want it to stop (and come back on when the device wakes up). However, it's currently going off no matter what. Here's how I register my alarm:
Intent updateIntent = new Intent(UPDATE_INTENT);
AlarmManager alarmService = (AlarmManager) context.getSystemService(
Context.ALARM_SERVICE);
PendingIntent updatePendingIntent = PendingIntent.getBroadcast(context, 0,
updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmService.setInexactRepeating(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), UPDATE_INTERVAL, updatePendingIntent);
The alarm manager docs say that RTC will not wake up the device. The docs specify exactly the behavior that I want:
Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
When I press the lock button on the device, I clearly see the going to sleep message from PowerManager in logcat:
I/PowerManagerService(488): Going to sleep by user request...
And then my alarm goes off anyway. What's going on here?
Ironically, every other question I've found on SO deals with alarms NOT going off while the device is asleep. I wish I had their problem!!
However, it's currently going off no matter what.
Presumably something else is holding a partial WakeLock, and the device is not actually asleep, even though the screen may be off. Use adb shell dumpsys power to try to track it down (look for the "Wake Locks" section).
I eventually decided to register broadcast receivers to listen for SCREEN_ON, and SCREEN_OFF, and toggle the alarm appropriately. I realize this might not be super elegant, but at least it always works even if another app is holding a wake lock.
Listening for screen on and off: android: broadcast receiver for screen on and screen off
Turning off an alarm: How to stop an alarm in android

BroadcastReceiver and AlarmManager at Device Boot

i've got a Activity in which an alarm is started at a time which can be choosen by the user; the time is stored in a database.
Than a BroadcastReceiver is called which fires a notification at the specific time.
So everything is working, but now i want to start the alarms also when the device is rebooted and i don't know how exactly i can do this.
According to this site Alarm Notification i should implement an other BroadcastReceiver which starts when the device is booted and fires the alarms like i do in my other BroadCastReceiver.
But on all other sites, they advice to implement a BroadcastReceiver which starts an extra service and than fires the alarms.
Also i wonder if it's possible that the BroadcastReceiver which could be started when the device is booted does have access to my database in which the date and time of the alarms to be fired are stored.
Thanks everybody
Yes, as it's part of the same application it has access to the database. So you could receive notification of device boot then set your alarms up using the time in the database, or fire the alarms. Or both.
Hope this helps.

how to get notification of Alarm which is passed in between switchoff to switched on mobile

Hi
I am developing a application in which i am using AlarmManager
Problem
When i set pending Intent using Alarm manager on perticular date and time its work fine
but suppose i set alarm time on date 30-05-2011 and time 10:00 AM and suppose current time is date 30-05-2011 and time 09:50 AM
and now after creating pending intent i switched off my device and after 10:01 AM i starts my device
at that time i expect notification for 10:00 AM alarm but i am not getting it
Any idea how i can get notification After switch on my mobile
Through AlarmManager, you can only wake up your device when it is sleeping.
To do that use
setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
or set(...)
with RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP
But it doesn't work with a device at off.
So you should consider having your alarms in a database, along with the last time your app was on, and count the alarms you missed since last time you were up.
Regards,
Stéphane
If you read the AlarmManager API doc page:
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
As an alternative, you can register a broadcast receiver for the intent android.intent.action.BOOT_COMPLETED and check your SharedPreferences if you need to perform an action.
See this question for details about the broadcast: Trying to start a service on boot on Android

Categories

Resources