AlarmManager object after turning off and on the phone - android

In my app, I set an alarm
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
...
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
It works fine unless I turn off and turn on the phone.
To be more specific, let's say at 10:20, I set an alarm to 10:22 and I turn off and turn on the phone at 10:21, alarm won't work.
What might be the problem? Is that a broadcast issue of the pendingIntent there or should I set some flags of the alarmManager object for it to work in such conditions?

The documentation about the AlarmManager says that :
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.
It seems that the AlarmClock included by default by Android does work even after a reboot.
On way to keep your alarms working after a reboot, is to start your application on boot completed and set up all the alams again with the AlarmManager.
(In fact you may want to just setup your alarms using a Broadcast, not start your app)
Here is a StackOverflow question dealing about lunching an app on startup.
You wan also check out how the default AlarmClock does this by reading from the source.
You can read and download it from here

Related

Registering an alarm in Android, where does it go?

I have an Android application which provides users in a particular city info on food and drink deals. I would like to create an alarm so that a user can click "Remind Me" and when a particular deal begins (such as 2pm) the app opens to that specific info.
Here is the code that creates my alarm:
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ViewListingActivity.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
alarmMgr.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
Question:
Where in my phone can I see this alarm registered? I looked in the Clock -> Alarm but I do not see it.
I just want to see it so I know it has been setup correctly.
Where in my phone can I see this alarm registered? I looked in the Clock -> Alarm but I do not see it.
I think you've got a wrong idea about the concept of AlarmManager :
AlaramManager is an android system component allowing you (as a developer) to schedule execution of one of the three:
1) start service
2) send broadcast
3) start activity.
one of the best thing is that your app don't have to be running at all. instead, the the OS will wake up your process if your application scheduled with the AlaramManager any alarm for this time, and start the service/activity/broadcast based on when you specified..
Registering an alarm in Android, where does it go?
the pendingIntent object you provided to the alramManager is saved by the system, in order to execute it at the right time.
there is no any system UI component that shows you that visually!! and there is not make sense that there will be such, because - app can schedule alarms for any kind of reasons that most of them not directly been visualized to the user.
for example: my app schedule alram each two hours, in order to update in front of my company servers, the local database of my app. this action don't involved any interaction from the user, and happens completely in background.
after understanding that, I can tell you that there is way to check programatically what scheduled alarms your app have in front of the AlaramManager: follow this answer: How to check if AlarmManager already has an alarm set?
for more information, I strongly recommends you to read the documention of PendingIntent , and AlaramManager:
http://developer.android.com/reference/android/app/AlarmManager.html
http://developer.android.com/reference/android/app/PendingIntent.html
You can do it by using ADB. From the ADB console within Android Studio try the following:
adb shell dumpsys alarm
This should show you all of the alarms that have been set, as well as their details, such as alarm time, interval, etc.

AlarmManager stopped when screen off

I am trying to develop android app; and I use AlarmManager to do something.
but AlarmManager stopped when screen off
and when screen on AlarmManager will work properly
how I solve this issue?
The AlarmManager can use one the following four types when setting an alarm:
ELAPSED_REALTIME
ELAPSED_REALTIME_WAKEUP
RTC
RTC_WAKEUP
When using options 1 or 3, the AlarmManager will not launch the PendingIntent if the device is asleep (screen is off + a few seconds).
Try using option 2 or 4 to send the scheduled PendingIntent even when the device is asleep.

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

how to show notification on device start up

I am developing notification app in android that shows notification on particular date and time that i set using date picker. It shows correctly if my app is running in background. But when i force stop my app or if device switched off &restarted, notification does not show. How to show notification even if app force closed or device restarted.
You can't. Notifications are attached to application's context. If the application is killed/destroyed, your notification also goes away with it.
What you may do is to re-create those notifications once your application or it's service is started. For that, make sure you do catch android.intent.action.BOOT_COMPLETED broadcast in order to implement this automatically.
You should consider using AlarmManager instead of a service.
Set an alarm at the desired date/time with a custom intent. In the BroadcastReceiver, you create and show the Notification.
If the Device is restarted, you might need also to listen to the BOOT_COMPLETED Intent and reset the alarms.
EDIT:
An example:
long time = // time in milliseconds of when you want your Alarm
PendingIntent mIntent = PendingIntent.getBroadcast(context,
0, new Intent("YOUR_CUSTOM_INTENT"), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
time, mIntent );
Then Catch the YOUR_CUSTOM_INTENT intent in a BroadcastReceiver, show the notification and set the following alarm.

Text to speech when phone is asleep - Android

I have an app that uses text to speech to inform the user every 10 mins that 10 mins have passed. It currently works fine but if you sleep the phone (press the power button) it no longer plays the sound.
How can i play these sounds even when the phone is asleep?
In general, your code is not running if divice goes to sleep. In order to make your code running you need to acquire WakeLock from PowerManager. But in your case you don't need to have the WakeLock acquired all the time. You need to wake you application every 10 minutes. Otherwise your app will just eat battery doing nothing.
In order to wake your application periodically you need to use a special Android's AlarmManager.
Here is an example:
Intent myIntent = new Intent(getApplicationContext(), YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, pendingIntent);
You also may send broadcast which you will process in your Service (if you don't want to use Activity).
EDIT: Playback will not start unless you explicitly create a SCREEN_DIM_WAKE_LOCK. Note that PARTIAL_WAKE_LOCK will not work with playback (don't now why, probably it's a bug).
PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Music");
wakeLock.acquire();
...start playback...
wakeLock.release();
EDIT: Added project that shows an example of running a playback every 60 seconds (even when screen is off and usb cable is disconnected). It can be found here: http://code.google.com/p/playevery60/

Categories

Resources