0

I need your help to resolve this problem :

Context.startForegroundService() did not then call Service.startForeground()

I'm using a BroadcastReceiver to start the notification service :

@Override
public void onReceive(Context context, Intent intent) {
    WakeLock.acquire(context);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ContextCompat.startForegroundService(context, new Intent(context, StartNotificationService.class).putExtras(intent));
    } else {
        context.startService(new Intent(context, StartNotificationService.class).putExtras(intent));
    }
}

Have you any idea please ? How can I resolve this issue ? Thanks you

Mohamed
  • 2,312
  • 4
  • 20
  • 30
  • 1
    You have to call `startForeground` from within the foreground service. This was added I believe in Oreo (not too sure). You have a time window to do this. If you don't call it from within that time, Android throws that exception. – Fred May 08 '19 at 14:49

1 Answers1

2

Your Service must call startForeground() when it is started, otherwise the system will not allow it to run.

Larry Schiefer
  • 15,551
  • 2
  • 26
  • 32