4

My app will call startForegroundService(intent) in the onCreate of the MainActivity. And I put startForeground(ON_SERVICE_CONNECTION_NID, notification) in both the onCreate and the startCommand of the Service. But I'm still receiving this error occasionally.

Exception android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1775)
android.os.Handler.dispatchMessage (Handler.java:105)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:6541)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

How could this happen? Is it possible that the onCreate or startCommand doesn't get called in 5 seconds? If so, how should we call startForegroundService with no error?

Updated 2017-12-09

I don't know why every one said that it's the same as this question: Context.startForegroundService() did not then call Service.startForeground() You can even find a comment from me over there.

The reason why it's different is what you can see at the first line of this question. I have put the startForegroundService and startForeground at the right place, but it's still showing this error randomly.

And Here's the Google issue tracker: https://issuetracker.google.com/issues/67920140

Stopping the service before this happens will cause a crash.

This is another problem about the lifecycle of the service. The assertion may be incorrectly triggered after the service closed.

Kimi Chiu
  • 2,103
  • 3
  • 22
  • 37
  • can you post some code here – UltimateDevil Oct 18 '17 at 06:02
  • I think it's clear enough to explain the problem with plain text. Could you tell me what do you want to know? So I can add more infomations. – Kimi Chiu Oct 18 '17 at 06:06
  • see this answer https://stackoverflow.com/questions/44425584/context-startforegroundservice-did-not-then-call-service-startforeground exact question is here – UltimateDevil Oct 18 '17 at 06:15
  • @UltimateDevil, Read my question here, and you can find out that I've already put `startForeground` in the `onCreate` of the `Service`. Just so you know, I've commented under the "Answer" over there. – Kimi Chiu Oct 18 '17 at 09:36
  • Possible duplicate of [Context.startForegroundService() did not then call Service.startForeground()](https://stackoverflow.com/questions/44425584/context-startforegroundservice-did-not-then-call-service-startforeground) – Anup Dec 09 '17 at 10:15
  • 3
    @Anup It's not the same. I've commented under that answer in your link and @UltimateDevil's link. I've already put the `startForegroundService` and `startForeground` in the right place as I described in my question. – Kimi Chiu Dec 09 '17 at 10:43
  • @KimiChiu Ridiculous that they closed your bug! – swooby Mar 21 '18 at 23:03
  • I opened a new one with a clear repro at https://issuetracker.google.com/issues/76112072 – swooby Mar 21 '18 at 23:35
  • 1
    @swooby Thanks! Actually, I've created another thread already. And that one was closed too. Even if I provided all the informations and attachments they needed. They said it "Works as Intended". After then, I figure out that the `startForegroundService(intent)` shouldn't be inside `onCreate` of Activities. So I move it to `onPause` which only promotes the service to foreground after the Activity is in background state. And the `startForeground(ON_SERVICE_CONNECTION_NID, notification)` should only be inside `onCreate` of the service. Finally the error report stops. – Kimi Chiu Mar 22 '18 at 14:45

3 Answers3

0

I am facing same issue and after spending time found a solutons you can try below code. If your using Service then put this code in onCreate else your using Intent Service then put this code in onHandleIntent.

if (Build.VERSION.SDK_INT >= 26) {
    String CHANNEL_ID = "my_app";
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
            "MyApp", NotificationManager.IMPORTANCE_DEFAULT);
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("")
            .setContentText("").build();
    startForeground(1, notification);
}

and call this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(getSyncIntent(context));
    } else {
        context.startService(getSyncIntent(context));
    }
Sambhaji Karad
  • 387
  • 7
  • 18
0

i had the same problem.
What helped me in the end, was to initialize the NotificationChannel as early as possible ( i did it in the application class itself) and then create and use the notification in the service.

I put this in the Application class:

private void createNotificationChannel() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence name = "MyStreamingApplication";
        String description = "radio";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.setSound(null, null);
        mChannel.enableVibration(false);
        mChannel.setDescription(description);
        notificationManager.createNotificationChannel(mChannel);
    }
}

and in the onStartCommand method of the service i call:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        startForeground(NOTIFICATION_ID, createNotification());
    } 
Realerik
  • 1
  • 1
0

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.

Milo
  • 3,365
  • 9
  • 30
  • 44