Приложение выдает сбой, когда служба хочет остановить

Я запустил такую ​​службу:

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

Но когда я хочу остановить эту службу, я столкнулся с ошибкой:

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6501)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Чтобы устранить эту ошибку, я попробовал: 1. Использовал stopSelf(); в методе onDestroy (). 2. Использовал startForeground(0, new Notification()); в методе onCreate ().

Но ошибка все равно остается. Я также много искал на официальных сайтах StackOverflow и Android. Не могу найти подходящего ответа для решения моей проблемы. Кстати, эта ошибка запускается с версии Android OREO.

Попросите вас проверить это stackoverflow.com/questions/44425584/…

AskNilesh 18.09.2018 08:01

проверьте здесь - Issuesetracker.google.com/issues/76112072

Sachin 18.09.2018 08:02

Nilesh Rathod: Да, я это проверил, но мне это не помогло.

Ali Ahmed 18.09.2018 08:04

на какой версии Android вылетает?

Vladyslav Matviienko 18.09.2018 08:10

Это начало с OREO 8.0

Ali Ahmed 18.09.2018 08:11
0
5
130
1

Ответы 1

Как уже упоминалось здесь

"Android 8.0 introduces the new method startForegroundService() to start a new service in the foreground. After the system has created the service, the app has five seconds to call the service's startForeground() method to show the new service's user-visible notification. If the app does not call startForeground() within the time limit, the system stops the service and declares the app to be ANR."

Другие вопросы по теме