- Why Start the Service on Android O and encounter a problem record Context.startForegroundService did not then call Service.startForeground
- android.app.RemoteServiceException: Context.startForegroundService() did not then call #4634
- Comments
- bapjg commented Oct 9, 2019
- Actual behaviour
- Expected behaviour
- Steps to reproduce
- Environment data
- Web server error log
- Nextcloud log (data/nextcloud.log)
- android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() #549
- Comments
- Borna-f commented Dec 22, 2020
- gotev commented Dec 22, 2020
- Borna-f commented Dec 23, 2020
- Borna-f commented Dec 23, 2020
- Borna-f commented Dec 24, 2020
- gotev commented Dec 24, 2020 •
- Context.startForegroundService did not then call Service.startForeground #1104
- Comments
- virengujariya commented Aug 31, 2018 •
- rantianhua commented Sep 1, 2018 •
- virengujariya commented Sep 3, 2018 •
- rantianhua commented Sep 8, 2018
- Jeffin21 commented Sep 9, 2018
- rantianhua commented Sep 10, 2018
- rantianhua commented Sep 10, 2018
- Jeffin21 commented Sep 10, 2018 •
- rantianhua commented Sep 10, 2018
- Jeffin21 commented Sep 10, 2018
Why Start the Service on Android O and encounter a problem record Context.startForegroundService did not then call Service.startForeground
Recording scenarios: Android 8.0 has a complex feature; the system does not allow background applications to create background services. Therefore, Android 8.0 introduces a completely new method, Context.startForegroundService(), to start a new service in the foreground.
After the system creates the service, the app has 5 seconds to call the service’s startForeground() method to display the user-visible notification for the new service. If the app does not call startForeground() within this time limit, the system will stop the service and declare the application as ANR.
However, when calling: context.startForegroundService(intent), the following ANR is reported. The startForegroundService() document indicates that startForeground() is called after the service is started.
and run App see Error Log
Solution: After starting the service with startForegroundService, call startForeground() in the service’s onCreate method.
and try ran again
Note that Notification.Builder(Context, String) is used in api 26, and the Id of NotificationChannel is to be specified. If not added, it will prompt: Developer warning for package XXX, Failed to post notification on channel “null”.
You have followed the group:
ใครสนใจ มา join line ได้ครับ รับจำนวนจำกัด ใครปัญหา ชอบแชร์ มีเรื่องอะไรใหม่ๆ สามารถ join เข้าได้เลยครับ 😁
หากสนใจ ก็สามารถ join ได้ที่นี้เลยครับ . หรือ scan qrcode . ไปได้เลย
[Android Github Dev] คุณได้รับคำเชิญให้เข้าร่วมสแควร์ของ LINE
https://line.me/ti/g2/UVVDK6Z5EE
Android Developer
Thank you for joining: https://www.facebook.com/groups/883546485084033/?fref=ts I created a group of Android Developers Android and Kotlin Droidcon Github library. If you have any questions, you can ask. You can join in the App Telegram. https://t.me/joinchat/IhB5KQ0aXC7ckNgjRaBaCw Now join Android Developers And Kotlin Droidcon Github Library Community Telegram App to help each other. Joining Link:
เข้าร่วม Group Line: เข้าร่วมกลุ่ม
แฟนเพจ PongPloy Zone AppDev
Language learning application APP .
Practice writing, reading, Kai-ABC, this application. Designed to be easy to use, uncomplicated, with illustrations and sound for train children to read according to Thai-English consonants clearly.Practice writing all 44 Thai consonants from chicken to hawk and 26 English consonants since A-Z makes it easy to learn and remember.This application is suitable for Thai students. And foreigners studying Thai languageAnd in the future, may add more games to children
ฝึกเขียน อ่าน ก ไก่-ABC แอพพลิเคชั่นนี้ ออกแบบมาให้ใช้งานง่าย ไม่ซับซ้อน โดยมีภาพประกอบพร้อมเสียงสำหรับฝึก ให้เด็กๆ ท่องตาม อ่านเสียงพยัญชนะ ไทย — อังกฤษ ได้ชัดเจน ฝึกเขียนพยัญชนะไทยทั้ง 44 ตัว ตั้งแต่ ก ไก่ จนถึง ฮ นกฮูก และ พยัญชนะภาษาอังกฤษทั้ง 26 ตัว ตั้งแต่ A-Z ทำให้ง่ายต่อการเรียนรู้และจดจำแอพพลิเคชั่นนี้เหมาะสำหรับเด็กนักเรียนไทย และชาวต่างชาติที่ศึกษาภาษาไทยและต่อไปในอนาคต อาจจะเพิ่ม เกม ให้เด็กมาสนใจมากขึ้น
Источник
android.app.RemoteServiceException: Context.startForegroundService() did not then call #4634
Comments
bapjg commented Oct 9, 2019
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord<1d139ca u0 com.nextcloud.clientcom.owncloud.android.services.operationsservice>
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2126)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
************ APP INFORMATION ************
ID: com.nextcloud.client
Version: 30080090
Build flavor: gplay
************ DEVICE INFORMATION ************
Brand: HONOR
Device: HWBKL
Model: BKL-L09
Id: HUAWEIBKL-L09S
Product: BKL-L09
************ FIRMWARE ************
SDK: 28
Release: 9
Incremental: 329C432
Actual behaviour
- got error message : Nextcloud crashed
Expected behaviour
None in particular
Steps to reproduce
Environment data
Stock or customized system:
Nextcloud app version:
Nextcloud server version:
Web server error log
Nextcloud log (data/nextcloud.log)
NOTE: Be super sure to remove sensitive data like passwords, note that everybody can look here! You can use the Issue Template application to prefill some of the required information: https://apps.nextcloud.com/apps/issuetemplate
The text was updated successfully, but these errors were encountered:
Источник
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() #549
Comments
Borna-f commented Dec 22, 2020
Hi,
I sometimes get the following error when the upload finishes and it crashes my app. Any idea or help? The last part after — is the error. I brought the previous logs to get you an idea of when it does happen. (It is related to uploadservice/UploadService).
I/UploadService: UploadService — (uploadId: N/A) — Service is about to be stopped because idle timeout of 10s has been reached
I/UploadService: NotificationActionsObserver — (uploadId: N/A) — unregistered
I/UploadService: UploadService — (uploadId: N/A) — Stopping foreground execution
I/UploadService: UploadService — (uploadId: N/A) — UploadService destroyed
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: myApp, PID: 5855
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord<5986d70 u0 myappnet.gotev.uploadservice.uploadservice>
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2182)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8107)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
The text was updated successfully, but these errors were encountered:
gotev commented Dec 22, 2020
Thank for reporting this @Borna-f 😉
Can you replicate it in an Android instrumented test?
Borna-f commented Dec 23, 2020
When making a Pull Request (and running workflows) the android-tests (probably also sometimes) fails with the following error:
net.gotev.uploadservice.BinaryUploadTests > successfulBinaryUploadAfterOneRetry[test(AVD) — 10] FAILED
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord<377d41b u0 net.gotev.uploadservice.testnet.gotev.uploadservice.uploadservice>
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
Tests on test(AVD) — 10 failed: Instrumentation run failed due to ‘Process crashed.’
Task :uploadservice:connectedDebugAndroidTest FAILED
Task :uploadservice-s3:compileDebugJavaWithJavac
Task :uploadservice-ftp:connectedDebugAndroidTest
Starting 5 tests on test(AVD) — 10
FAILURE: Build failed with an exception.
- What went wrong:
Execution failed for task ‘:uploadservice:connectedDebugAndroidTest’.
So somewhere the services are having issues, causing the app to crash. Is that helpful/enough information?
Borna-f commented Dec 23, 2020
Just to clarify. It is not specified to the specific test mentioned in the previous comment. This crash can happen at any task. i.e. here is another log I’ve got from another task
net.gotev.uploadservice.BinaryUploadTests > userCancelledBinaryUpload[test(AVD) — 10] FAILED
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord<4a1a50 u0 net.gotev.uploadservice.testnet.gotev.uploadservice.uploadservice>
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
Tests on test(AVD) — 10 failed: Instrumentation run failed due to ‘Process crashed.’
Task :uploadservice-s3:mergeDebugAndroidTestResources
Task :uploadservice:connectedDebugAndroidTest FAILED
Task :uploadservice-s3:processDebugAndroidTestResources
Task :uploadservice-ftp:connectedDebugAndroidTest
Starting 5 tests on test(AVD) — 10
FAILURE: Build failed with an exception.
- What went wrong:
Execution failed for task ‘:uploadservice:connectedDebugAndroidTest’.
I see my S3 name somewhere there. Can it be something related to my newly added module?
Borna-f commented Dec 24, 2020
gotev commented Dec 24, 2020 •
@Borna-f Thank you for the details 😉
When making a Pull Request (and running workflows) the android-tests (probably also sometimes) fails with the following error
I also spot this happening very rarely on CI builds, but always thought it to be some random CI problem, like the emulator disappearing unexpectedly sometimes during the execution 😄
What I can’t still understand is how come that error happens after the upload is finished. It would have made much more sense to me to debug it if it happened at the start. We should find a way to replicate this in the tests. It may be a service lifecycle implementation problem.
In Android 8.0+ docs is stated that when you launch a service in the background with startForegroundService you have max 5 seconds to launch the startForeground , otherwise you get android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
In this case though you get this error after the upload is finished. I thought about the following two cases:
- the upload is so fast that it happens in under 5 seconds and everything is finished. This can be investigated by looking at the logs (https://stackoverflow.com/questions/44425584/context-startforegroundservice-did-not-then-call-service-startforeground/46449975#comment83431904_46449975)
- the problem happens in the interval after the upload is finished and Service’s stopSelf is called, so long after the initial stages of service execution
Let’s dive in the code and continue from there.
startForeground is done here:
Lines 145 to 165 in 2f179c3
/* * |
* Check if the task is currently the one shown in the foreground notification. |
* @param uploadId ID of the upload |
* @return true if the current upload task holds the foreground notification, otherwise false |
*/ |
@Synchronized |
fun holdForegroundNotification ( uploadId : String , notification : Notification ): Boolean < |
if ( ! UploadServiceConfig .isForegroundService) return false |
if (foregroundUploadId == null ) < |
foregroundUploadId = uploadId |
UploadServiceLogger .debug( TAG , uploadId) |
> |
if (uploadId == foregroundUploadId) < |
startForeground( UPLOAD_NOTIFICATION_BASE_ID , notification) |
return true |
> |
return false |
> |
stopForeground is done here:
Lines 167 to 187 in 2f179c3
/* * |
* Called by each task when it is completed (either successfully, with an error or due to |
* user cancellation). |
* @param uploadId the uploadID of the finished task |
*/ |
@Synchronized |
fun taskCompleted ( uploadId : String ) < |
val task = uploadTasksMap.remove(uploadId) |
// un-hold foreground upload ID if it’s been hold |
if ( UploadServiceConfig .isForegroundService && task != null && task.params.id == foregroundUploadId) < |
UploadServiceLogger .debug( TAG , uploadId) |
foregroundUploadId = null |
> |
if ( UploadServiceConfig .isForegroundService && uploadTasksMap.isEmpty()) < |
UploadServiceLogger .debug( TAG , NA ) |
stopForeground( true ) |
shutdownIfThereArentAnyActiveTasks() |
> |
> |
the following happens at a lifecycle level starting from UploadService (skipping previous execution stages from intent creation to service invocation on purpose as the problem is not there):
Источник
Context.startForegroundService did not then call Service.startForeground #1104
Comments
virengujariya commented Aug 31, 2018 •
What problem do you get?
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord
Which version of FileDownloader are you using when you produce such problem?
1.7.5
How to reproduce such problem?
When OS >= Oreo and App is still in foreground
Do you set FileDownloadLog.NEED_LOG=true ?
Yes
I think problem here is that in FileDownloadService class you check for needMakeServiceForeground with conditions Build.VERSION.SDK_INT >= Build.VERSION_CODES.O and !isAppOnForeground() However, if OS >= Oreo then it should always call makeServiceForeground() doesn’t matter if app is in foreground or not.
If you think that’s correct then I can open PR
The text was updated successfully, but these errors were encountered:
rantianhua commented Sep 1, 2018 •
The restriction is for background app to start background service, if call makeServiceForground during app is in foreground, there always is a notification for FileDownloader, however, I think it’s not so necessary.
virengujariya commented Sep 3, 2018 •
@rantianhua But we must have to call makeServiceForground if device has OS >= Oreo with notification attached. That’s restriction introduced by OS. Otherwise under certain cases it will crash (error I mentioned at first)
isAppOnForeground shouldn’t be checked.
rantianhua commented Sep 8, 2018
I will consider that and do more tests, thanks!
Jeffin21 commented Sep 9, 2018
getting lot of ANR’s in 8.0 and 9.0 devices , please provide a fix. Thank you !!
rantianhua commented Sep 10, 2018
@Jeffin21 can you confirm the cause of ANR?
rantianhua commented Sep 10, 2018
@virengujariya Can you provide a whole FileDownloader Log?
Jeffin21 commented Sep 10, 2018 •
rantianhua commented Sep 10, 2018
@Jeffin21 What do you mean? These log had provided by @virengujariya . I want to know the cause of your ANR.
Jeffin21 commented Sep 10, 2018
I published my app in playstore, got anr for this issue
Источник