How to place a kill safe alarm fired on daily basis? - android

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.

Related

Schedule daily alarm after app install

I want to execute a BroadcastReceiver once a day.
Scheduling an alarm when the device boots works well, but it requires the device to be rebooted at least once.
How can I schedule the alarm right after app install (and still persist the scheduling after reboot) ?
How can I schedule the alarm right after app install
Wait for the user to launch one of your activities from the home screen, then schedule your alarm on the first run of your app.
Until then, or until something else uses an explicit Intent to work with one of your app components, your app will not run, and so you have no opportunity to schedule an alarm.
When you are working with AlarmManager, the alarm will always reset when the device rebooted, then you will always have to re-schedule the task after the reboot.
So, i strongly recommend:
Use shared preferences to store the scheduler time.
Find the broadcast receiver that suits to your task (https://developer.android.com/reference/android/content/Intent.html)
the ACTION_PACKAGE_INSTALL was depreacted, you could use the "ACTION_PACKAGE_ADDED" for detect when the package was installed.
You can schedule the task after detect the installation.
For persisting the scheduling after reboot, you could use a BroadcastReceiver to detect a reboot and re-initialize whatever is needed (hopefully).
http://www.tutorialspoint.com/android/android_broadcast_receivers.htm

Android reschedule alarmmanager alarm when app is killed

I've developed an app to schedule multiple local notifications to remind users to do something. Every month in the current year there should an notification be raised.
These local notifications are scheduled using an "AlarmManager". A notification is created and raised in the OnRetrieve of a "BroadcastReceiver".
It works all fine until the app is terminated (by user) or the device is rebooted.
After some research I found the solution to reschedule the alarm / local notifications if the device is rebooted => using a BroadcastReceiver with "ActionBootCompleted" as intent filter and then reschedule the notifications in the "OnReceive".
Unfortunately I can't find a decent solution to reschedule the alarm / local notifications if the app is terminated.
What is the best approach for this case?
Try to run this as a background service. When the user opens up the app for the first time, call the service OnCreate(). Make sure the service is START_STICKY so it cannot be stopped unless you explicitly tell it to. Then place your AlarmManagers inside the service.

Android Developer - Alarm manager vs service

I am making an app that needs to execute a function each hour even the app is closed.
First of all, I thought to create a service, but during my tests, I realise that android sometimes kills my service. So I was looking for another solution and I found AlarmManager. I have implemented it and it seems to work but I have the doubt if it will happen the same the service or it will run forever? (Until reboot of the mobile...)
Another question, it is necessary to create a new thread to execute the process in alarm manager or it runs directly in other thread?
I have implemented it and it seems to work but I have the doubt if it will happen the same the service or it will run forever? (Until reboot of the mobile...)
It will run until:
the device is rebooted, as you noted, or
the user uninstalls your app, or
you cancel the events yourself, or
the user goes into Settings, finds your app in the list of installed apps, taps on that entry, and clicks the Force Stop button
It's possible that alarms will need to be scheduled again after your app is upgraded (I forget...).
it is necessary to create a new thread to execute the process in alarm manager or it runs directly in other thread??
Unless the work you are going to do will take only a couple of milliseconds, you will want a background thread for it. That leads to two possible patterns:
If you are not using a _WAKEUP-style alarm, use a getService() PendingIntent to send control to an IntentService every hour
If you are using a _WAKEUP-style alarm, you will need to use a getBroadcast() PendingIntent, and have it either invoke your subclass of my WakefulIntentService, or you will need to manage a WakeLock yourself to keep the device awake while you do your bit of work
No, Android won't kill scheduled alarms and they got executed as planned unless app is replaced or device is rebooted. Use broadcast receivers for these events to reschedule Alarms. There's no way to prevent Force Stop as it kills all of your app components and threads totally.
That depends on what Alarm Manager do. If it sends a broadcast, the receiver limit is 10 second.
If it starts an Activity, Service or Intent Service, there is no limit. For Activity and Services you must finish or stop it and for Intent Services until the process is finished. Be aware that you can't have another thread inside Intent Service and you'r limited to code inside the OnHandleIntent.
Also you must consider device state. If it's sleep and you are using Wake Up flag receivers won't need a wake lock, but others do. It won't take long for device to go back to sleep.
Don't waste system resources with a service because Alarm Manager do what you want.

Alarm doesn't ring if program force-stopped

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

Android AlarmService when phone is shut down

When a user logs into my app I create an AlarmService to be fired in 10 minutes. In response to the alarm, I start a service that polls some data. After polling the service sets itself up with a new Alarm to fire again in another 10 minutes. This works great, but what exactly happens when the users phone gets shut off? Does the alarm wait until the phone is restarted? Or does the alarm get destroyed? How do I go about handeling this situation so the alarm can continue when the user restarts their phone?
you can't get an alarm work when the phone is off, but you can detect when the phone boot and do something in that moment, like calculate the time remaining to make something.
This link could help you:
Trying to start a service on boot on Android
Hope to help :)
If the user fully powers down the phone, your alarms are wiped out. Usually, the solution is to register a BroadcastReceiver to get control at boot time via the BOOT_COMPLETED broadcast, to go in and set up your alarm(s) again. Here is a sample app demonstrating this.

Categories

Resources