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

前端 未结 3 926
温柔的废话
温柔的废话 2021-01-13 21:38

My app will call startForegroundService(intent) in the onCreate of the MainActivity. And I put startForeground(ON_SERVICE_CONNEC

3条回答
  •  温柔的废话
    2021-01-13 22:11

    I experienced this issue for Galaxy Tab A(Android 8.1.0). In my application, I start a foreground service in the constructor of MainApplication(Application-extended class). With the Debugger, I found it took more than 5 second to reach OnCreate() of the service after startForegroundService(intent). I got crash even though startForeground(ON_SERVICE_CONNECTION_NID, notification) got called in OnCreate().

    After trying different approach, I found one works for me as the following:

    In the constructor, I used AlarmManager to wakeup a receiver, and in the receiver, I started the foreground service.

    I guessed that the reason I had the issue because heavy workload of application starting delayed the creation of the foreground service.

    Try my approach, you might resolve the issue as well. Good luck.

提交回复
热议问题