My service seems to restart when I close my app

390 views Asked by At

I have a service which creates a CountDownTimer and then displays a Notification and updates it according to CountDownTimer.

The problem comes when I close the app. Even if CountDownTimer is already half done, it will reset when app is completely closed.

This is the main part of Service cass:

/*
Variables
 */

private NotificationManager notificationManager;

private CountDownTimer countDownTimer;

private String TAG = "NotificationService";

private int NOTIFICATION_COUNTDOWN = 0;
private int NOTIFICATION_RANOUT = 1;

private int reservationDuration;

/*
Callbacks
 */

@Override
public void onCreate() {
    super.onCreate();

    notificationManager =
            (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    EventBus.getDefault().register(this);
    reservationDuration = 15;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    countDownTimer = new CountDownTimer(1000 * 60 * 15, 1000 * 60) {

        @Override
        public void onTick(long millisUntilFinished) {
            notificationManager.notify(NOTIFICATION_COUNTDOWN, createReservationActiveNotification(--reservationDuration));
        }

        @Override
        public void onFinish() {
            notificationManager.cancel(NOTIFICATION_COUNTDOWN);
            stopSelf();
        }
    }.start();

    return super.onStartCommand(intent, flags, startId);
}

The Service seems to completely restart whenever the app is closed. Because even the reservationDuration variables resets to 15 (whcih is in onCreate) and the Notification goes back to full time as soon as the app is completely destroyed/closed.

1

There are 1 answers

0
nPn On BEST ANSWER

The value that you return from your override of the onStartCommand determines what happens to your service when it is killed, for example when your app is killed do to the need for more memory.

If you return START_NOT_STICKY your service will not be restarted after being killed until you explicitly start it via a start command.

Since you are returning super.onStartCommand(intent, flags, startId); you are getting the default of either START_STICKY or START_STICKY_COMPATIBILITY. Which is documented here:

public int onStartCommand (Intent intent, int flags, int startId)

Added in API level 5 Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.

For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY.

Here are the details for various return values for onStartCommand (from the documentation).

public static final int START_CONTINUATION_MASK

Added in API level 5 Bits returned by onStartCommand(Intent, int, int) describing how to continue the service if it is killed. May be START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT, or START_STICKY_COMPATIBILITY.

Constant Value: 15 (0x0000000f) public static final int START_FLAG_REDELIVERY

Added in API level 5 This flag is set in onStartCommand(Intent, int, int) if the Intent is a re-delivery of a previously delivered intent, because the service had previously returned START_REDELIVER_INTENT but had been killed before calling stopSelf(int) for that Intent.

Constant Value: 1 (0x00000001) public static final int START_FLAG_RETRY

Added in API level 5 This flag is set in onStartCommand(Intent, int, int) if the Intent is a retry because the original attempt never got to or returned from onStartCommand(Intent, int, int).

Constant Value: 2 (0x00000002) public static final int START_NOT_STICKY

Added in API level 5 Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit call to Context.startService(Intent). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will not be re-started if there are no pending Intents to deliver.

This mode makes sense for things that want to do some work as a result of being started, but can be stopped when under memory pressure and will explicit start themselves again later to do more work. An example of such a service would be one that polls for data from a server: it could schedule an alarm to poll every N minutes by having the alarm start its service. When its onStartCommand(Intent, int, int) is called from the alarm, it schedules a new alarm for N minutes later, and spawns a thread to do its networking. If its process is killed while doing that check, the service will not be restarted until the alarm goes off.

Constant Value: 2 (0x00000002) public static final int START_REDELIVER_INTENT

Added in API level 5 Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int). This Intent will remain scheduled for redelivery until the service calls stopSelf(int) with the start ID provided to onStartCommand(Intent, int, int). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).

Constant Value: 3 (0x00000003) public static final int START_STICKY

Added in API level 5 Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.

Constant Value: 1 (0x00000001) public static final int START_STICKY_COMPATIBILITY

Added in API level 5 Constant to return from onStartCommand(Intent, int, int): compatibility version of START_STICKY that does not guarantee that onStartCommand(Intent, int, int) will be called again after being killed.

Constant Value: 0 (0x00000000)