8

I get the following exception output from Device Monitor:

//FATAL EXCEPTION: main
//Process: com.xxx.yyy, PID: 11584
//android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
//    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
//    at android.os.Handler.dispatchMessage(Handler.java:105)
//    at android.os.Looper.loop(Looper.java:164)
//    at android.app.ActivityThread.main(ActivityThread.java:6944)
//    at java.lang.reflect.Method.invoke(Native Method)
//    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
//    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

I have 4 services in my App and all of them basically looks like this:

public class MyService extends Service {

    private static final int forgroundId = 55544433; //Unique for each Service
    private Notification notification;

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

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

        initNotification(); //Creates a notification

        startForeground(forgroundId, notification); //Start foreground, very first thing..

        //Do Stuff

        return START_STICKY;
    }
}

To start the services, I use the following:

Intent i = new Intent(context, MyService.class);
i.SetAction("myaction..");
i.PutExtra(...);
android.support.v4.content.ContextCompat.startForegroundService(context, i);

I have a few other libraries which also might have services starting included in my project:

  • Fabric.io
  • Firebase Messaging/Analytics/etc.
  • Google Play Services Analytics/GCM/Tasks/etc.
  • Android-Job from Evernote

The problem is, I can not figure out exactly which service is causing this error. If the class was included in the exception, I wouldn't be here...

Is there anyway to [catch/throw/I don't know] any hint as to where the exception is coming from?

I have read all the following posts with no luck at all:

This happens only sometimes when opening the app, so to me my code is correct?

I only want to know how to figure out which service this is coming from.

Pierre
  • 8,397
  • 4
  • 64
  • 80
  • Are you able to reproduce this at all (even if only occasionally)? If so, you should be able to go back in Logcat and figure out what service was started recently. If you are only getting this from production users, though, that approach won't work. – CommonsWare Jun 06 '19 at 14:02
  • @CommonsWare Thank you for answering, by figuring out you mean add log lines for the services started and then see which one it might be? It does happen often but not directly reproducible. – Pierre Jun 06 '19 at 14:04
  • "you mean add log lines for the services started and then see which one it might be?" -- no, I think there are system log messages that report service starts. There certainly are for activity starts. – CommonsWare Jun 06 '19 at 14:06
  • @CommonsWare I got this line `W/ActivityManager(3651): Bringing down service while still waiting for start foreground: ServiceRecord{337d45e u0 com.xxx.yyy/mypackge.MyService}` and 5 seconds later, the above exception. The very first two lines in the `onStartCommand` is the `initNotification` then `startForeground` - So something else is causing these two lines never to be called in the first place, like keeping the CPU occupied? – Pierre Jun 06 '19 at 14:34
  • Since `onStartCommand()` is called on the main application thread, tying up the main application thread of the service's process would block `onStartCommand()` from being called. However, "Bringing down service" suggests that perhaps something is stopping the service -- do you have any code that does this? Might that code be being executed sometime between `startForegroundService()` and your `onStartCommand()` callback? – CommonsWare Jun 06 '19 at 14:42
  • @CommonsWare Interesting, it is only stopping itself after it has done its work. However, I do kill it when `onDestroy` or `onLowMemory` is called. So maybe I should add a flag `startedForeground` and only kill it in `onDestroy` and `onLowMemory` when the flag is true? – Pierre Jun 06 '19 at 14:50
  • 1
    Possibly. You might consider just getting rid of all of that and just have the service stop itself when the work is finished. Or, you might consider switching to `WorkManager` (or Android-Job, since you seem to be using that already), rather than rolling your own service for this. – CommonsWare Jun 06 '19 at 14:57
  • Thank you for your responses @CommonsWare – Pierre Jun 06 '19 at 14:58
  • 2
    @CommonsWare Removing `onDestroy` and `onLowMemory` which stopped the service fixed the issue for me – Pierre Jun 06 '19 at 15:27

1 Answers1

7

If you call startForegroundService(), you need to let the service start. Otherwise, you will get "Context.startForegroundService() did not then call Service.startForeground()" exception.

In your case, it appears that, on occasion, you would stop the service after you called startForegroundService() but before onStartCommand() on the service would get called. And, apparently, that means the service winds up never getting started. That's arguably a framework bug, but we are unlikely to get that changed, so we need to deal with it.

One approach is to not use your own service. For "fire and forget" sorts of work like this, use WorkManager, or possibly JobIntentService, and you no longer need to worry about foregrounding.

If you want to stick with the foreground service, though, let the service start. One way to do that is to have the service stop itself. For example, a service can have its own onLowMemory() method, where it can stop itself. The service knows whether the service called startForeground() yet or not and can handle that more gracefully than can an outside party.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    It's not my case. I don't stop the service directly. Instead, I send a broadcast "Close services" that registered in "onStartCommand" (calling "startForeground") of the service. So that never happens the sevice is stoped before calling "startForeground". But I still see that error is reported sometimes. – Huy Duong Tu Oct 03 '19 at 04:13