How to save scheduled alarm after app was killed by Android or task killer?

Code that schedules alarm.

    PendingIntent sender = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, time, sender);

Its work fine, but when I kill my app in task killer, I lost my scheduleed alarm. How to solve this problem?

  • FloatingActionButton class could not be instantiated
  • recording and saving audio on Android
  • update sql database with ContentValues and the update-method
  • How to output LogCat to Console?
  • “The item you were attempting to purchase could not be found” Android in-app billing
  • Android PCM Bytes
  • Related posts:

    Android widget not updating - has incorrect widget ID
    How to Add Click event in Stack Layout or Frame
    Selecting/highlighting multiple items in listview with custom adapter - Android
    RecyclerView: Async image-loading
    When to use synchronized in Java
    Live video streaming application on Android
  • Snackbar hides the Floating Action on pressing the action button on it
  • Android ContentProvider calls bursts of setNotificationUri() to CursorAdapter when many rows are inserted with a batch operation
  • completely transparent status bar and navigation bar on lollipop
  • How to detect the stock Android browser
  • What adapter shall I use to use HashMap in a ListView
  • netstack: LIB_MGR - Error loading lib libdnshostprio.so
  • 2 Solutions collect form web for “How to save scheduled alarm after app was killed by Android or task killer?”

    have your application broadcast a message as its being killed, and when this message is broadcast, then have a listener check if the service is still running.. if its not run it. This will insure that your service is running even if the application is killed.

    Update

    I’ll try to create a flow diagram for you

    Death/Restart of a service

    The onDestroy() method is part of a service.

    I hope this helps.

    UPDATE 2

    One thing I forgot to mention is the fact that you ideally only want one instance of the service to be run. So just looking at the ID that is present within the onStart() should be == to 1 to start it else.. ignore it.

    Methods of notice of the Service Class:

    onStart() : This method is called when the service is being started

    onDestroy() : This is the method that is called when a service is being killed

    Methods of notice of the BroadcastReciever class:

    onReceive(): This methods receives all intents that are sent to it (unless filtered)

    Look up examples on BroadcastRecievers (Message Broadcasting) and Service (Starting a service)

    References:

    http://developer.android.com/reference/android/content/BroadcastReceiver.html

    http://developer.android.com/reference/android/app/Service.html

    Alarm set by alarm manager is not killed when app is closed, how ever when a reboot occurs all alarms are cleared by the os since there is no persistence. So you need to do the persistence.

    • Every Time while setting a alarm save the alarm time.
    • Register a receiver for boot completion.
    • Set the alarm again on reboot.

      public class BootReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(Context context, Intent intent) {
              //re register the alarm
         }
      }
      

    Manifest.xml

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    .......
    <receiver
          android:name="BootReceiver"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    

    You could use SharedPreference to save the time (time at when the alarm should be triggered or time at when it should be triggered next)

    Use that to set a new alarm at the boot receiver.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.