casolorz
casolorz

Reputation: 9554

Context.startForegroundService() did not then call Service.startForeground() even though I stopped the service

So my app has some remote actions that trigger a service and notification. Between the call to startForegroundService and the time the service tries to start the notification things could change so the service once again checks the state of things and then decides what to do.

So if my service decides it doesn't need to run it will call:

stopForeground(true);
stopSelf();

But for some reason this doesn't seem to work because I get this exception almost immediately after making those calls.

11-16 13:34:23.488 15099-15099/mypackage E/AndroidRuntime: FATAL EXCEPTION: main
Process: mypackage, PID: 15099
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

So how can I solve this?

Thanks.

Edit:

I created a sample project that all it does is call startForegroundService when the Activity starts and then it does this on the service:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"start");
        stopForeground(true);
        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }

And it crashes, whether I use stopForeground(true) or not.

Edit: This seems to fix it, but seems really ugly to have to create a fake notification just to cancel it.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"start");
        String CHANNEL_ID = "my_channel_01";
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this, CHANNEL_ID)

                        .setContentTitle("My notification")
                        .setContentText("Hello World!");

        startForeground(-1,mBuilder.build());
        stopForeground(true);
        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }

Upvotes: 14

Views: 4168

Answers (1)

Mateusz Kaflowski
Mateusz Kaflowski

Reputation: 2367

I have the same problem and I use the same workaround before stopSelf():

private void fakeStartForeground() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setContentTitle("")
                        .setContentText("");

        startForeground(NOTIFICATION_ID, builder.build());
}

I think that somebody form Google should give us some solution. Have you posted in Google Bugs reporting forum?

Upvotes: 8

Related Questions