3

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) ?

sdabet
  • 18,360
  • 11
  • 89
  • 158

3 Answers3

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

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:

  1. Use shared preferences to store the scheduler time.
  2. 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.

Thiago Souto
  • 761
  • 5
  • 13
0

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

Dawid Czerwinski
  • 670
  • 5
  • 11
  • That's what I'm doing so far. My concern is the post-install scheduling (before the device gets rebooted) – sdabet Aug 22 '16 at 15:07