Schedule daily alarm after app install - android

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

Related

How to restart alarms from AlarmManager when Task Manager kills application

I am making an Android application where I schedule alarms in AlarmManager that trigger notifications to the user and need to go off at rigid specific times.
When a user uses a task killing program (usually from a chinese phone and ROM), the alarms are killed as well.
This is troublesome, because after this happens, no more notifications are launched untill I re-open the app or restart the phone. This is not trivial to the target user that is a layman.
These alarms are supposed to work offline, so using GCM to re-up the alarms through a network listener is not an option. I actually need some "unkillable" service on the phone that checks if the alarms still exist and reschedule them if they don't. Is this possible?
I found this post here, but the last answer was in 2012: Keep android alarms alive, even after process killed by a task manager
Is there currently a solution to this?

When (if ever) the repeating AlarmManager's alarm is cancelled by the OS?

I've written the app which uses the AlarmManager to schedule events. It is able to create AlarmManager.setRepeating and cancel AlarmManager.cancel repeating alarms. I'm wondering on what conditions these alarms may be canceled by the Android OS (not from my code). For example what will happen if I create the alarm and then uninstall my app? Shall the alarm be cancelled?
As I know, yes installing the app will cancel your alarms ... also restarting your phone.
but for the second one you can refrish your alarms when device turn on.
Regards

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.

How to make a robust scheduled event?

I want my application to trigger an event at a given time. At the moment I'm using AlarmManager. But this will be lost if the users phone is restarted, or the user uses a task killer or ends the application with the android task manager.
So what is the best way to do this. Should I just use an alarm and have it repeatedly set in a service so when it is deleted it come right back?
You can use awake alarm service on boot completed so that all the task will be re scheduled after reboot. You can store alarm id and details in database.
Might be overkill for what you need, but check out CommonWare's WakefulIntentService:
https://github.com/commonsguy/cwac-wakeful
Even if you don't want to fire up an entire IntentService when your alarm is triggered, his code should give you some idea of how you can persist your alarms across device reboots.

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

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.

Categories

Resources