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.
Related
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.
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
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
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
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/