Android 13 ForegroundServiceDidNotStartInTimeException

1.3k views Asked by At
Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{f294fe6 u0 file_path}
   at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2272)
   at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2243)
   at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException()
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2522)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loopOnce(Looper.java:223)
   at android.os.Looper.loop(Looper.java:324)
   at android.app.ActivityThread.main(ActivityThread.java:8634)
   at java.lang.reflect.Method.invoke(Method.java)

Getting this crash for few users after app target sdk was updated to 33

enter image description here

All for android 13 and mostly happens when app is in background, not device specific

I have startForeground(id, notification) soon after getting intent at onStartCommand

1

There are 1 answers

0
Mohammad Azizi On

This happen when you call starforgroundservice(intent) and then you call startforground(int,notification) on oncreate when the operation takes more time to execute then it throw this exeption or you start and stop service immediately,,,,

Try to call startforground oncreate because you have only 5s to call it otherwise system will stop service and don't start and stop service immediately

Use starforgroundservice() for both stoping and starting service to ensure that we are stoping service when service is completely started

MyApplication.returnContext().startForegroundService(new Intent(MyApplication.returnContext(), IndicatorService.class).putExtra("stops",true));

And then add check in oncommand

if(intent.getBooleanExtra("stops",false)){
                stopSelf();
            }

Hope this help you