Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expedite foreground promotion #1024

Merged
merged 8 commits into from
Jun 7, 2021
Merged

Conversation

davidgyoung
Copy link
Member

@davidgyoung davidgyoung commented Apr 28, 2021

This moves the BeaconService call to promote itself to a foreground service to the first line in onCreate. This is done in an attempt to avoid crashes initiated by Android caused by the failure to promote a service to a foreground service within 5 seconds. Such an exception looks like this:

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2ce3f6e u0 com.myapp/org.altbeacon.beacon.service.BeaconService}
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1801)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:210)
    at android.app.ActivityThread.main(ActivityThread.java:7124)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:898)

Because the service is started on the main thread, it is possible that if that thread is very busy or blocked (potentially by other apps!) then it may take a long time to even call this onCreate method. In some cases where the main thread is busy or blocked, it may not be possible to do this in 5 seconds. Forum posts are full of claims from crash logs that this happens sometimes.

This is no guaranteed fix. But moving this to the top of the method gives the best thing that this library can do to prevent this crash. The app developer needs to take care not to initialize this library to start scanning in a case where the main thread could be blocked or overly busy. In other words, don't initialize beacon scanning then immediately do a large amount of work on the main thread.

This is in the 2.19-beta version of the library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant