Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
like image 294
casolorz Avatar asked Nov 16 '17 19:11

casolorz


People also ask

When should I call context startforegroundservice()?

The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.

What's wrong with startforegroundservice?

If you use startForegroundService (), the Service throws the following error. Context.startForegroundService () did not then call Service.startForeground () What's wrong with this? IOW, please provide a minimal reproducible example. That would include the entire Java stack trace and the code that is triggering the crash.

What happens after calling startforeground() unbindservice()?

After calling startForeground () unbindService (), the service is sometimes destroyed by system, and immediately started again. When the service initializes a worker thread during startup, we get an Error java.lang.InternalError: Thread starting during runtime shutdown :- (

Is startforeground in onstartcommand(...) necessary?

However, it is not necessary as onStartCommand (...) is called when the Service is created for the first time or called subsequent times after. onStartCommand (...) startForeground in onStartCommand (...) is important in order to update the Service once it has already been created.


1 Answers

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?

like image 55
Mateusz Kaflowski Avatar answered Oct 24 '22 07:10

Mateusz Kaflowski