23

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 working fine, but when I kill my app in task killer, I lost my scheduleed alarm. How to solve this problem?

Divers
  • 9,531
  • 7
  • 45
  • 88
  • I am trying the scenario any solution is available this problem? – mahesh Aug 12 '17 at 10:38
  • Are you using any power saver apps (Greenify ) or are you using force close?Some times it may be due to some built in power saving technique like in huawei (where a app needs to listed as protected to keep running)? – Sree Vishnu Aug 21 '17 at 08:42

2 Answers2

21

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

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • 2
    Can your write code example, please? I dont understand why this service die with app. Documentation says "Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running" – Divers May 06 '11 at 21:08
  • 1
    This doesn't include the death of your service. – JoxTraex May 06 '11 at 21:10
  • Thank you. Very nice diagram, but I dont have idea how to realize this. Should I extend AlarmManager class? – Divers May 06 '11 at 21:28
  • @user632516 this diagram is very easy to understand if you have idea about broadcast receiver. @Jox brilliant way to explain answer. but i have one doubt and that is if we register a receiver in onDestroy() than we also need to unregister it somewhere otherwise it will be a memory leak. could you explain little bit more. – Varundroid May 06 '11 at 21:52
  • You dont have to do any programatic registering, just use the intent filter that is available in the manifest.xml and attach an intent filter to that BroadcastReciever with the action you want to capture. And thanks to all of you! – JoxTraex May 07 '11 at 00:07
  • 2
    **JoxTraex** Did you ever try what are you say? Seems that onDestroy method don't invoke when APP force closed form task killer. – Divers May 07 '11 at 09:49
  • onDestroy() is always killed before the service is actually killed. Please make sure that you are properly broadcasting your messaging to your broadcastreciever class. This should work. – JoxTraex May 08 '11 at 21:52
  • 1
    I add Log.e("", "text") int onDestroy method. Its doesn't invoke when app or service killed from task killer. – Divers May 12 '11 at 10:12
  • @Divers, have you already solved this problem? Now I'm trying to keep safe my app service from task killers and stuck with same problem too as you. – Mario Jul 04 '11 at 21:14
  • @Divers - "application is not currently running" actually means that the application is in background. – Deb Jul 31 '15 at 02:32
  • 1
    It does not work, can't catch onDestroy(). It is not guaranteed that onDestroy() will be called: http://stackoverflow.com/questions/7236782/is-an-android-service-guaranteed-to-call-ondestroy – Jemshit Dec 17 '15 at 14:37
  • 3
    This answer should not be the accepted one. `onDestroy()` won't be called if the process is killed. See this answer from CommonsWare to a similar question: "There are various scenarios in which `onDestroy()` of an activity or service will not be called. This is one of them" http://stackoverflow.com/a/3595641/858626 – Jose_GD May 20 '16 at 21:57
8

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.

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Sree Vishnu
  • 385
  • 2
  • 13