I'm having the Context.startForegroundService() did not then call Service.startForeground()
in my Android service, but I cannot figure out why it is happening.
My application is for media streaming, and this error only occurs when you pause from the foreground notification (turning it into a regular notification), and you then swipe the notification away, which is intended to stop my service.
Here is the only method where the startForegroundService
, startForeground
and stopForeground
methods are called:
private void configureServiceState(long action) {
if (action == PlaybackStateCompat.ACTION_PLAY) {
if (!mServiceInStartedState) {
ContextCompat.startForegroundService(
StreamingService.this,
new Intent(
StreamingService.this,
StreamingService.class));
mServiceInStartedState = true;
} startForeground(NOTIFICATION_ID,
buildNotification(PlaybackStateCompat.ACTION_PAUSE));
} else if (action == PlaybackStateCompat.ACTION_PAUSE) {
stopForeground(false);
NotificationManager mNotificationManager
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert mNotificationManager != null;
mNotificationManager
.notify(NOTIFICATION_ID,
buildNotification(PlaybackStateCompat.ACTION_PLAY));
} else if (action == PlaybackStateCompat.ACTION_STOP) {
mServiceInStartedState = false;
stopForeground(true);
stopSelf();
}
}
And here is where the delete intent of my notification is set:
.setDeleteIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_STOP));
This configureServiceState(long action)
method is called only from my MediaSession
callbacks: onPlay
, onPause
and onStop
... obviously with the action being the intended action to be performed.
The error doesn't occur when performing onStop
from my UI, or when calling onPause
followed by onStop
from the UI (mirroring the action required to clear the notification), only from the notification.
All I can find about this error is that it supposedly occurs when you call startForegroundService
but don't call startForeground
within 5 seconds... however the startForeground
is invoked immediately after the only time startForegroundService
is invoked.
Additionally, an onPlaybackStateChange
is going to my 'Now Playing' activity in the onStop
method, which triggers that activity to run finish()
so that the service is not being restarted that way.
What am I missing here?
Additional details:
The service is not being partially restarted by my 'now playing' activity as code execution never reaches any of its methods.
The code execution also never seems to re-enter
configureServiceState
before the error is producedIf I add a breakpoint at the last possible point (
MediaButtonReceiver.handleIntent(mMediaSession, intent);
inonStartCommand
of my service), pausing execution here and trying to debug causes the debugger to disconnect shortly after pausingTrying a different notification channel for the foreground vs regular notification doesn't make a difference either
Swiping the paused notification away from the lock screen does not cause an error, it only occurs if the phone is unlocked; regardless of whether my app is actually open
Full exception:
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
My testing device is running Android 8.0, the project min API is 21 and target API is 27. Device API is 26.