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:
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.
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.