Android O - Old start foreground service still working?

So, with Android O, you need to have your service running as a foreground service if you want to receive more than just a few location updates per hour.

I noticed that the old method for starting a foreground service does seem to work on O. i.e.

startForeground(NOTIFICATION_ID, getNotification());

According to the behaviour changes guide here: https://developer.android.com/preview/behavior-changes.html

The NotificationManager.startServiceInForeground() method starts a foreground service. The old way to start a foreground service no longer works.

Though the new method only works when targeting O, it seems that the old method still seems to work on an O device whether targeting O or not.

Edit Including example:

The Google sample project LocationUpdatesForegroundService actually has a working example where you can see the issue first hand. https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService

The startForeground method seems to work without issue whether targeting and compiling against API level 25 OR targeting and compiling against O (as directed to here: https://developer.android.com/preview/migration.html#uya)

So, to reproduce:

  1. Configure the app gradle as mentioned in the previous link
  2. Open the app
  3. Request location updates
  4. Close app (either via back button or home button)

Service is running in foreground (shown by icon in notification shade). Location updates are coming through as expected (every 10 seconds) even on a device running O. What I am missing here?


Solution 1:

This worked for me.

  1. In Activity class, start service using startForegroundService() instead of startService()
    Intent myService = new Intent(this, MyService.class);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(myService);
    } else {
        startService(myService);
    }
  1. Now in Service class in onStartCommand() do as following
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ......
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(text)
                .setAutoCancel(true);

        Notification notification = builder.build();
        startForeground(1, notification);

    } else {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(text)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true);

        Notification notification = builder.build();

        startForeground(1, notification);
    }
    return START_NOT_STICKY;
}

Note: Using Notification.Builder instead of NotificationCompat.Builder made it work. Only in Notification.Builder you will need to provide Channel ID which is new feature in Android Oreo.

Hope it works!

EDIT:

If you target API level 28 or higher, you need FOREGROUND_SERVICE permission otherwise, your app will crash.

Just add this to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Solution 2:

In the Activity (or any context that starts the foreground service), call this:

Intent intent = new Intent(this, MyService.class)
ContextCompat.startForegroundService(context, intent);

When the service has started, create a notification channel using similar code to what Android docs say, and then create a builder and use it:

final Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID).setSmallIcon(...)//
            .setPriority(...).setCategory(...).setContentTitle(...).setContentText(...).setTicker(...);
// and maybe other preparations to the notification...
startForeground(notificationId, builder.build());

Solution 3:

Usually you start your service from a broadcast receiver using startService. They are saying that it's no more possible (or reliable) to call startService because there are now background limitations, so you need to call startServiceInForeground instead. However from docs it's not really clear when it happens because the app is whitelisted when it receives a broadcast intent, so it's not clear exactly when startService throws IllegalStateException.