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.

9
  • 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. Commented Jun 6, 2019 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. Commented Jun 6, 2019 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. Commented Jun 6, 2019 at 14:06
  • 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. Commented Jun 6, 2019 at 14:57
  • 2
    @CommonsWare Removing onDestroy and onLowMemory which stopped the service fixed the issue for me Commented Jun 6, 2019 at 15:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.