Context.startForegroundService () did not call Service.startForeground

My application will call startForegroundService(intent)in onCreate MainActivity. And I put startForeground(ON_SERVICE_CONNECTION_NID, notification)both in onCreateand in startCommand Service. But I still get this error periodically.

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 onCreateor is startCommandnot called after 5 seconds? If so, what should one call startForegroundServicewithout error?

Updated 2017-12-09

I don’t know why everyone said that it is the same as this question: Context.startForegroundService () did not call Service.startForeground () You can even find a comment from me there.

, , - , . startForegroundService startForeground , .

Google: https://issuetracker.google.com/issues/67920140

, , .

, . .

+4
3

, , , , . Service, onCreate , Intent Service, 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);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(getSyncIntent(context));
    } else {
        context.startService(getSyncIntent(context));
    }
0

.
NotificationChannel ( ), .

:

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);
    }
}

onStartCommand , :

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

Galaxy Tab A (Android 8.1.0). MainApplication ( ). , OnCreate() startForegroundService(intent) 5 startForegroundService(intent). , startForeground(ON_SERVICE_CONNECTION_NID, notification) OnCreate().

, :

AlarmManager AlarmManager , .

, , , , - .

, . .

0

Source: https://habr.com/ru/post/1687696/


All Articles