I have an alarm app which I think was programmed in the standard way using an AlarmManager and BroadcastReceiver. I have even added code so that the alarm stays active after a reboot. I have been using the program without problem for weeks now.
I had been led to believe that doing things this way meant that it was not essential to keep my alarm program running the whole time, but to my horror I have just found that if I set an alarm and then do a force stop of my alarm app, then the alarm never rings.
I'm not sure that this means I made a mistake in my coding, or I was misled into believing that it is possible to keep the alarm set despite the app being stopped.
Maybe the OS somehow detects that my alarm app has an alarm running and therefore avoids shutting it down? So as long as the user doesn't manually do a force stop then all should be well.
Any ideas?
EDIT: I just tested "Alarm Clock Xtreme". I set an alarm. Then did a force stop... the alarm never rang.
This is the expected behavior if your application is force closed. However, the alarm should continue working as expected if the user leaves the application through normal means like the back or home buttons.
See: Android alarm is cancelled after closing the application
Related
I'm learning Android and want to write some kind of count-down timer application, so I get a ring-tone after certain minutes are elapsed. The timer also should work if the user has closed the application or switched to another one. If my application is running, it should be able to display the remaining time.
I've tried with CountDownTimer, but this seems only to work when the phone is activated, but not like the alarms which could ring you up at the morning. What other similar API alternatives are there to activate the device if the time is elapsed?
You can use AlarmManager for this purpose.
count-down and alarm are two very different things (even thou both count time).
Count-down you probably want to run a service and put a notification with the flag ongoing = true updating the value of the time.
Alarm you want to use the AlarmManager (as pointed out by #PgmFreek) that you can schedule a specific time that the system will call an Intent for you.
I am developing an application that uses a Service as Contdown. When the user starts the countdown from the activity and the activity goes in background after the sleep button is pressed, I am using this Service to continue the countdown. When the count is finished the Service shows a notification with ringtone.
I use wait() "to count" the time in the Service. The strange behavior occurs when I use the application on a real device, but in debug mode. When Eclipse debugger is attached, the Service works well; when I test the application on the device without Eclipse debugger attached, the Service doesn't show the notification when the countdown is finished, unless the sleep button is repressed and the monitor is activated - then the notification and the ringtone are activated.
Can anyone can explain what causes this strange behavior? Maybe the issue is is related to Wake lock or a similar construct?
I use wait() "to count" the time in Service.
That is poor programming practice. Time elapses even without your tying up RAM to do it.
Anyone can explaine why this strange behavior?
The device fell asleep. This is normal, and desirable, behavior, to conserve battery life. With the USB cable plugged in, the device does not need to fall asleep, and if you checked the appropriate option in Developer Options, the device specifically will not fall asleep while plugged in.
Maybe is connected with Wake lock
Please do not use a WakeLock to keep the device awake for you to watch the clock tick by. Please use AlarmManager to get control when the countdown period is over. You can use a _WAKEUP-style alarm to arrange to wake up the device, and your BroadcastReceiver that gets control at that point can "launch a notification and ringtone". As a bonus, you can get rid of your service, so that your app can be better behaved on the user's device.
wait() calls are not guaranteed to wait for the right amount of time if the device goes to sleep. You should use AlarmManager to trigger your countdown timer instead.
In my app, I have to make it vibrate in time intervals defined by the user.
Basically it has a chronometer that makes the device vibrate when it has reached the defined time. Currently I do that by reading the chronometer value and comparing it with the desired value, in the onChronometerTick() method
I need this app to work in the background too. I've googled and found some examples about the android Service class.
My doubt is how my Service class will get the chronometer value? I've seen that the Service class can't handle layout components.
How can I do this? Is service the best way to handle this issue?
Thank you!
It sounds like what you want is an alarmmanager. Here's what it says regarding alarm managers:
These allow you to schedule your application to be run at some point
in the future. When an alarm goes off, the Intent that had been
registered for it is broadcast by the system, automatically starting
the target application if it is not already running. 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.
So if you're going to want the counter to continue after turning it off, you'll have to find an on-boot option or something. But yes, this is what you need to schedule events if you want them to keep happening after the screen locks.
I'm writing a simple Widget for Android which displays information which changes for every day. So the widget needs to be refreshed on midnight. The whole refreshing is implemented as a service and runs nicely, the problem is the invocation:
The only solution I found is to use the AlarmManager to a add an exact reoccurring timer on midnight each day. Then aquire a partial Wake-Lock, to make sure the device stays awake and run the code. This should work as expected but due to the usage of the wake lock, I am waking the device, so I am searching for a slimmer version:
There is no need to wake the device up on exact midnight, it is enough if I receive a timer event the first time the Device is up again on a new day. If the device is sleeping, nobody can look on the widget, so it is ok if the widget updates whenever the device is switched on again.
In other words: How do I run a service on the first moment of a day when the device is not sleeping, thus preventing a wakeup? I still need the device to stay awake than for period of time.
How can this bis done?
You can tell the AlarmManager to delay your invocation until the device wakes up anyway.
Then don't use a WakeLock in the.
I know how to setup an alarm to fire repeatedly, but the alarm stops firing after my application is killed.
How do I make sure the alarm continues to fire as it was setup?
You can automatically restart your closed app Activity after a few
seconds using the OneShotAlarm approach described at
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/OneShotAlarm.html
using a scheduled app (re)launch.
If you try to kill the integrated clock application, you'll see the alarm doesn't ring anymore (I had the problem with my HTC Desire).
That's because you can't keep any observer when the application is killed.
So you can't place any kill safe alarm.