Android 9.0: Not allowed to start service: app is in background.. after onResume()Android 8.0: java.lang.IllegalStateException: Not allowed to start service IntentHow to check if a service is running on Android?Trying to start a service on boot on AndroidIn GCM registration my app is crashingGoogle Analytics service Intent crashes on Android OreoAndroid 8.0: java.lang.IllegalStateException: Not allowed to start service IntentBackground Notification in Firebase: Android OreoSecurityException: Not allowed to start ServiceOreo - Widget services and broadcast receiver: Not allowed to start service IntentNot allowed to start service Intent - Android OreoStoring events to a database while app is running in background
Help, I cannot decide when to start the story
Shifting tenses in the middle of narration
Do I have to cite common CS algorithms?
Why aren’t there water shutoff valves for each room?
How can God warn people of the upcoming rapture without disrupting society?
Go to last file in vim
How would armour (and combat) change if the fighter didn't need to actually wear it?
Is this n-speak?
How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?
Link for download latest Edubuntu
Are there examples in Tanach of 3 or more parties having an ongoing conversation?
Is there a way to proportionalize fixed costs in a MILP?
Cases with long math equation
Are there really no countries that protect Freedom of Speech as the United States does?
Are there any cons in using rounded corners for bar graphs?
Running code generated in realtime in JavaScript with eval()
Why did IBM make the PC BIOS source code public?
How can I communicate my issues with a potential date's pushy behavior?
When did Bilbo and Frodo learn that Gandalf was a Maia?
Do beef farmed pastures net remove carbon emissions?
What is a "soap"?
Word for an event that will likely never happen again
Dogfights in outer space
What is the farthest a camera can see?
Android 9.0: Not allowed to start service: app is in background.. after onResume()
Android 8.0: java.lang.IllegalStateException: Not allowed to start service IntentHow to check if a service is running on Android?Trying to start a service on boot on AndroidIn GCM registration my app is crashingGoogle Analytics service Intent crashes on Android OreoAndroid 8.0: java.lang.IllegalStateException: Not allowed to start service IntentBackground Notification in Firebase: Android OreoSecurityException: Not allowed to start ServiceOreo - Widget services and broadcast receiver: Not allowed to start service IntentNot allowed to start service Intent - Android OreoStoring events to a database while app is running in background
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've got a music player which attempts to start a Service
in onResume()
of an Activity
. I've removed a few lines for clarity, but the code is effectively:
@Override
protected void onResume()
super.onResume();
startService(new Intent(this, MusicService.class));
According to the crash logs, this is throwing an Exception on some devices running Android P:
Caused by java.lang.IllegalStateException: Not allowed to start service Intent cmp=another.music.player/com.simplecity.amp_library.playback.MusicService : app is in background uid UidRecord6a4a9c6 u0a143 TPSL bg:+3m25s199ms idle change:cached procs:1 seq(1283,1283,1283)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at com.simplecity.amp_library.utils.MusicServiceConnectionUtils.bindToService(SourceFile:36)
at com.simplecity.amp_library.ui.activities.BaseActivity.bindService(SourceFile:129)
at com.simplecity.amp_library.ui.activities.BaseActivity.onResume(SourceFile:96)
How is it possible that my app is in the background, immediately after onResume()
(and super.onResume()
) is called?
This doesn't make any sense to me. Could this be a platform bug? All 3500+ users affected by this crash are on Android P.
android android-service android-9.0-pie
|
show 13 more comments
I've got a music player which attempts to start a Service
in onResume()
of an Activity
. I've removed a few lines for clarity, but the code is effectively:
@Override
protected void onResume()
super.onResume();
startService(new Intent(this, MusicService.class));
According to the crash logs, this is throwing an Exception on some devices running Android P:
Caused by java.lang.IllegalStateException: Not allowed to start service Intent cmp=another.music.player/com.simplecity.amp_library.playback.MusicService : app is in background uid UidRecord6a4a9c6 u0a143 TPSL bg:+3m25s199ms idle change:cached procs:1 seq(1283,1283,1283)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at com.simplecity.amp_library.utils.MusicServiceConnectionUtils.bindToService(SourceFile:36)
at com.simplecity.amp_library.ui.activities.BaseActivity.bindService(SourceFile:129)
at com.simplecity.amp_library.ui.activities.BaseActivity.onResume(SourceFile:96)
How is it possible that my app is in the background, immediately after onResume()
(and super.onResume()
) is called?
This doesn't make any sense to me. Could this be a platform bug? All 3500+ users affected by this crash are on Android P.
android android-service android-9.0-pie
7
I don't have a good answer for you but I can confirm we're seeing this as well. We've never reproduced it in-house, but similarly we're seeing it when starting the service in onResume(). I suspect this is a bug in Android P.
– kcoppock
Aug 25 '18 at 2:51
8
OK, glad it's not just me. This issue has been reported: issuetracker.google.com/issues/113122354
– Tim Malseed
Aug 25 '18 at 2:59
try in manifiest add process name in service tag
– Ashvin solanki
Aug 25 '18 at 5:03
3
Also, Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. From developer.android.com/about/versions/pie/android-9.0-changes-28
– Access Denied
Oct 8 '18 at 9:13
2
@iaindownie Yes, it appears to have worked, it's been in prod for a short while now, and new instances that I hadn't fixed (didn't know about at the time) started showing up since it made it further along.
– Ben987654
Jan 7 at 17:13
|
show 13 more comments
I've got a music player which attempts to start a Service
in onResume()
of an Activity
. I've removed a few lines for clarity, but the code is effectively:
@Override
protected void onResume()
super.onResume();
startService(new Intent(this, MusicService.class));
According to the crash logs, this is throwing an Exception on some devices running Android P:
Caused by java.lang.IllegalStateException: Not allowed to start service Intent cmp=another.music.player/com.simplecity.amp_library.playback.MusicService : app is in background uid UidRecord6a4a9c6 u0a143 TPSL bg:+3m25s199ms idle change:cached procs:1 seq(1283,1283,1283)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at com.simplecity.amp_library.utils.MusicServiceConnectionUtils.bindToService(SourceFile:36)
at com.simplecity.amp_library.ui.activities.BaseActivity.bindService(SourceFile:129)
at com.simplecity.amp_library.ui.activities.BaseActivity.onResume(SourceFile:96)
How is it possible that my app is in the background, immediately after onResume()
(and super.onResume()
) is called?
This doesn't make any sense to me. Could this be a platform bug? All 3500+ users affected by this crash are on Android P.
android android-service android-9.0-pie
I've got a music player which attempts to start a Service
in onResume()
of an Activity
. I've removed a few lines for clarity, but the code is effectively:
@Override
protected void onResume()
super.onResume();
startService(new Intent(this, MusicService.class));
According to the crash logs, this is throwing an Exception on some devices running Android P:
Caused by java.lang.IllegalStateException: Not allowed to start service Intent cmp=another.music.player/com.simplecity.amp_library.playback.MusicService : app is in background uid UidRecord6a4a9c6 u0a143 TPSL bg:+3m25s199ms idle change:cached procs:1 seq(1283,1283,1283)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at com.simplecity.amp_library.utils.MusicServiceConnectionUtils.bindToService(SourceFile:36)
at com.simplecity.amp_library.ui.activities.BaseActivity.bindService(SourceFile:129)
at com.simplecity.amp_library.ui.activities.BaseActivity.onResume(SourceFile:96)
How is it possible that my app is in the background, immediately after onResume()
(and super.onResume()
) is called?
This doesn't make any sense to me. Could this be a platform bug? All 3500+ users affected by this crash are on Android P.
android android-service android-9.0-pie
android android-service android-9.0-pie
edited Feb 8 at 13:58
Tim Malseed
asked Aug 25 '18 at 2:29
Tim MalseedTim Malseed
3,7774 gold badges35 silver badges55 bronze badges
3,7774 gold badges35 silver badges55 bronze badges
7
I don't have a good answer for you but I can confirm we're seeing this as well. We've never reproduced it in-house, but similarly we're seeing it when starting the service in onResume(). I suspect this is a bug in Android P.
– kcoppock
Aug 25 '18 at 2:51
8
OK, glad it's not just me. This issue has been reported: issuetracker.google.com/issues/113122354
– Tim Malseed
Aug 25 '18 at 2:59
try in manifiest add process name in service tag
– Ashvin solanki
Aug 25 '18 at 5:03
3
Also, Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. From developer.android.com/about/versions/pie/android-9.0-changes-28
– Access Denied
Oct 8 '18 at 9:13
2
@iaindownie Yes, it appears to have worked, it's been in prod for a short while now, and new instances that I hadn't fixed (didn't know about at the time) started showing up since it made it further along.
– Ben987654
Jan 7 at 17:13
|
show 13 more comments
7
I don't have a good answer for you but I can confirm we're seeing this as well. We've never reproduced it in-house, but similarly we're seeing it when starting the service in onResume(). I suspect this is a bug in Android P.
– kcoppock
Aug 25 '18 at 2:51
8
OK, glad it's not just me. This issue has been reported: issuetracker.google.com/issues/113122354
– Tim Malseed
Aug 25 '18 at 2:59
try in manifiest add process name in service tag
– Ashvin solanki
Aug 25 '18 at 5:03
3
Also, Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. From developer.android.com/about/versions/pie/android-9.0-changes-28
– Access Denied
Oct 8 '18 at 9:13
2
@iaindownie Yes, it appears to have worked, it's been in prod for a short while now, and new instances that I hadn't fixed (didn't know about at the time) started showing up since it made it further along.
– Ben987654
Jan 7 at 17:13
7
7
I don't have a good answer for you but I can confirm we're seeing this as well. We've never reproduced it in-house, but similarly we're seeing it when starting the service in onResume(). I suspect this is a bug in Android P.
– kcoppock
Aug 25 '18 at 2:51
I don't have a good answer for you but I can confirm we're seeing this as well. We've never reproduced it in-house, but similarly we're seeing it when starting the service in onResume(). I suspect this is a bug in Android P.
– kcoppock
Aug 25 '18 at 2:51
8
8
OK, glad it's not just me. This issue has been reported: issuetracker.google.com/issues/113122354
– Tim Malseed
Aug 25 '18 at 2:59
OK, glad it's not just me. This issue has been reported: issuetracker.google.com/issues/113122354
– Tim Malseed
Aug 25 '18 at 2:59
try in manifiest add process name in service tag
– Ashvin solanki
Aug 25 '18 at 5:03
try in manifiest add process name in service tag
– Ashvin solanki
Aug 25 '18 at 5:03
3
3
Also, Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. From developer.android.com/about/versions/pie/android-9.0-changes-28
– Access Denied
Oct 8 '18 at 9:13
Also, Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. From developer.android.com/about/versions/pie/android-9.0-changes-28
– Access Denied
Oct 8 '18 at 9:13
2
2
@iaindownie Yes, it appears to have worked, it's been in prod for a short while now, and new instances that I hadn't fixed (didn't know about at the time) started showing up since it made it further along.
– Ben987654
Jan 7 at 17:13
@iaindownie Yes, it appears to have worked, it's been in prod for a short while now, and new instances that I hadn't fixed (didn't know about at the time) started showing up since it made it further along.
– Ben987654
Jan 7 at 17:13
|
show 13 more comments
6 Answers
6
active
oldest
votes
There is a workaround from Google:
The issue has been addressed in future Android release.
There is a workaround to avoid application crash. Applications can get
the process state in Activity.onResume() by calling
ActivityManager.getRunningAppProcesses() and avoid starting Service if
the importance level is lower than
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the
device hasn’t fully awake, activities would be paused immediately and
eventually be resumed again after its fully awake.
So I think it should like that:
// hack for https://issuetracker.google.com/issues/113122354
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses != null)
int importance = runningAppProcesses.get(0).importance;
// higher importance has lower number (?)
if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
URLPlayerService.startActionBroadcastServiceData(PlayerActivity.this);
I have used handler as a workaround and it works pretty good but not 100%:
// hack for https://issuetracker.google.com/issues/113122354
handler.postDelayed(() -> URLPlayerService.startService(PlayerActivity.this),200);
4
As said in issuetracker: "activities would be paused immediately and eventually be resumed again after its fully awake." so acitivity will be resumed again and process importance will be IMPORTANCE_FOREGROUND I think. so don't need to handler. is it true?
– David
Apr 13 at 6:19
add a comment |
UPDATE: This is working for us in Prod, but it's not 100%. I have received one crash report over the past month and a half when there would have been well over a hundred otherwise. Until this is properly fixed, this seems like our best option for now. Maybe if I raised the time beyond 300 that one crash would never have happened?
We're testing this out right now which so far seems to be working. Will update as we see more results
class ResumingServiceManager(val lifecycle: Lifecycle) : LifecycleObserver
init
lifecycle.addObserver(this)
val disposable: CompositeDisposable = CompositeDisposable()
fun startService(context: Context, intent: Intent)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
context.startService(intent)
else
Single.just(true)
.delaySubscription(300, TimeUnit.MILLISECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess =
context.startService(intent)
).addTo(disposable)
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopped()
disposable.clear()
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy()
lifecycle.removeObserver(this)
In onCreate()
initialize it and then anytime you want to start a service in onResume just call resumingServiceManager.startService(this, intent)
It's lifecycle aware so it will clear the disposable if it pauses cancelling the onSuccess from triggering when it might be on the way to the background with an immediate open/close.
1
For the Oreo+ case, would this be equivalent to something like:handler.postDelayed(context.startService(intent), 300)
and in thestopped()
, callinghandler.removeCallbacksAndMessages(null)
?
– Carmen
Feb 14 at 9:00
1
I believe that would function the same yes. Can always test it by adding some extra logging and set the time to a second or two the confirm the delay / proper removal and ability to trigger as you open/close the app repeatedly.
– Ben987654
Feb 14 at 16:00
Can anyone confirm non Rx version works posted above using handler.postDelayed
– Rinav
Jun 17 at 10:57
@rinav I would look at using googles official recommended solution at this point. I've taken out the lifecycle logic above and put their fix in the else branch instead of the Rx and it does prevent crashes. The only question is, is it completely reliable or not, which I don't know yet but we'll find out once we do a release soon. The Rx version above while it drastically reduced crashes, I still got 3-4 a month by the time I replaced every call, instead of 400-500. The good news though, is routing al the calls through this one file made it easy to swap fixes!
– Ben987654
Jun 17 at 15:53
add a comment |
our team faced with the same problem. My calendar shows 5 of April, 2019, but problem still reproduce on my Samsung Galaxy S9+, android 9.0 (One UI)
We start service in onResume and unbind onPause.
How to reproduce
Just lock your device, when activity with this logic is on the screen, and don't touch 10-15 minutes. After unlock screen, app will crash.
How to fix
We found solution that actually work. Since android 8+, start your service in android.os.Handler.post(...)
Example (Kotlin):
override fun onResume()
super.onResume()
Handler().post
val serviceIntent = Intent(activity, SomeService::class.java)
activity?.startService(serviceIntent)
activity?.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)
Good luck!
add a comment |
This has been marked as 'fixed' in the Android Issue Tracker:
- https://issuetracker.google.com/issues/110237673
- https://issuetracker.google.com/issues/113122354
Presumably the fix will be released in one of the Android Q releases.
According to the Googler who closed the issue,
There is a workaround to avoid application crash. Applications can get the process state in
Activity.onResume()
by callingActivityManager.getRunningAppProcesses()
and avoid starting Service if the importance level is lower thanActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
add a comment |
They fixed the crash (in some future release, not sure which exactly) and provided a workaround for now:
https://issuetracker.google.com/issues/110237673#comment9
add a comment |
Maybe android.arch.lifecycle could be used as a workaround for this Android 9 bug?
public class MyActivity extends Activity implements LifecycleObserver
protected void onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
startService(intent);
else
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
else
startService(intent);
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onEnterForeground()
startService(intent);
ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
I've found it here.
1
The lifecycle observer is just responding to events triggered by the activity. So this all really amounts to just moving thestartService()
call toonStart()
. As you say though, this might be a handy workaround.
– Tim Malseed
Oct 21 '18 at 23:36
1
Unfortunately I am seeing the same exception withLifecycle.State.STARTED
. I am going to tryLifecycle.State.RESUMED
and@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
which is called afteronResume()
and see if that fixes the issue.
– MarcinLe
Oct 22 '18 at 18:05
Thanks for sharing this workaround. Did you have any positive results when using Lifecycle.State.RESUMED and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)?
– Renso Lohuis
Oct 29 '18 at 10:47
I updated my app to use State.RESUMED. For a few days it was working fine, so I hoped this solves the issue, but I have just noticed another crash on android 9 again :(
– MarcinLe
Oct 30 '18 at 11:41
Yeah, not surprised at all. Again, Arch Lifecycle events are just triggered by the lifecycle events of the underlying Fragment/Activity, so this is no different to just moving thestartService
call intoonStart()
oronResume()
or wherever, unless you account for very minuscule timing delays. Sounds like moving the call toonStart()
is not a workaround.
– Tim Malseed
Oct 30 '18 at 23:26
|
show 2 more comments
protected by Community♦ Nov 8 '18 at 14:05
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a workaround from Google:
The issue has been addressed in future Android release.
There is a workaround to avoid application crash. Applications can get
the process state in Activity.onResume() by calling
ActivityManager.getRunningAppProcesses() and avoid starting Service if
the importance level is lower than
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the
device hasn’t fully awake, activities would be paused immediately and
eventually be resumed again after its fully awake.
So I think it should like that:
// hack for https://issuetracker.google.com/issues/113122354
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses != null)
int importance = runningAppProcesses.get(0).importance;
// higher importance has lower number (?)
if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
URLPlayerService.startActionBroadcastServiceData(PlayerActivity.this);
I have used handler as a workaround and it works pretty good but not 100%:
// hack for https://issuetracker.google.com/issues/113122354
handler.postDelayed(() -> URLPlayerService.startService(PlayerActivity.this),200);
4
As said in issuetracker: "activities would be paused immediately and eventually be resumed again after its fully awake." so acitivity will be resumed again and process importance will be IMPORTANCE_FOREGROUND I think. so don't need to handler. is it true?
– David
Apr 13 at 6:19
add a comment |
There is a workaround from Google:
The issue has been addressed in future Android release.
There is a workaround to avoid application crash. Applications can get
the process state in Activity.onResume() by calling
ActivityManager.getRunningAppProcesses() and avoid starting Service if
the importance level is lower than
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the
device hasn’t fully awake, activities would be paused immediately and
eventually be resumed again after its fully awake.
So I think it should like that:
// hack for https://issuetracker.google.com/issues/113122354
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses != null)
int importance = runningAppProcesses.get(0).importance;
// higher importance has lower number (?)
if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
URLPlayerService.startActionBroadcastServiceData(PlayerActivity.this);
I have used handler as a workaround and it works pretty good but not 100%:
// hack for https://issuetracker.google.com/issues/113122354
handler.postDelayed(() -> URLPlayerService.startService(PlayerActivity.this),200);
4
As said in issuetracker: "activities would be paused immediately and eventually be resumed again after its fully awake." so acitivity will be resumed again and process importance will be IMPORTANCE_FOREGROUND I think. so don't need to handler. is it true?
– David
Apr 13 at 6:19
add a comment |
There is a workaround from Google:
The issue has been addressed in future Android release.
There is a workaround to avoid application crash. Applications can get
the process state in Activity.onResume() by calling
ActivityManager.getRunningAppProcesses() and avoid starting Service if
the importance level is lower than
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the
device hasn’t fully awake, activities would be paused immediately and
eventually be resumed again after its fully awake.
So I think it should like that:
// hack for https://issuetracker.google.com/issues/113122354
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses != null)
int importance = runningAppProcesses.get(0).importance;
// higher importance has lower number (?)
if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
URLPlayerService.startActionBroadcastServiceData(PlayerActivity.this);
I have used handler as a workaround and it works pretty good but not 100%:
// hack for https://issuetracker.google.com/issues/113122354
handler.postDelayed(() -> URLPlayerService.startService(PlayerActivity.this),200);
There is a workaround from Google:
The issue has been addressed in future Android release.
There is a workaround to avoid application crash. Applications can get
the process state in Activity.onResume() by calling
ActivityManager.getRunningAppProcesses() and avoid starting Service if
the importance level is lower than
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the
device hasn’t fully awake, activities would be paused immediately and
eventually be resumed again after its fully awake.
So I think it should like that:
// hack for https://issuetracker.google.com/issues/113122354
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses != null)
int importance = runningAppProcesses.get(0).importance;
// higher importance has lower number (?)
if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
URLPlayerService.startActionBroadcastServiceData(PlayerActivity.this);
I have used handler as a workaround and it works pretty good but not 100%:
// hack for https://issuetracker.google.com/issues/113122354
handler.postDelayed(() -> URLPlayerService.startService(PlayerActivity.this),200);
edited Apr 8 at 14:05
answered Mar 27 at 11:23
Mateusz KaflowskiMateusz Kaflowski
76811 silver badges25 bronze badges
76811 silver badges25 bronze badges
4
As said in issuetracker: "activities would be paused immediately and eventually be resumed again after its fully awake." so acitivity will be resumed again and process importance will be IMPORTANCE_FOREGROUND I think. so don't need to handler. is it true?
– David
Apr 13 at 6:19
add a comment |
4
As said in issuetracker: "activities would be paused immediately and eventually be resumed again after its fully awake." so acitivity will be resumed again and process importance will be IMPORTANCE_FOREGROUND I think. so don't need to handler. is it true?
– David
Apr 13 at 6:19
4
4
As said in issuetracker: "activities would be paused immediately and eventually be resumed again after its fully awake." so acitivity will be resumed again and process importance will be IMPORTANCE_FOREGROUND I think. so don't need to handler. is it true?
– David
Apr 13 at 6:19
As said in issuetracker: "activities would be paused immediately and eventually be resumed again after its fully awake." so acitivity will be resumed again and process importance will be IMPORTANCE_FOREGROUND I think. so don't need to handler. is it true?
– David
Apr 13 at 6:19
add a comment |
UPDATE: This is working for us in Prod, but it's not 100%. I have received one crash report over the past month and a half when there would have been well over a hundred otherwise. Until this is properly fixed, this seems like our best option for now. Maybe if I raised the time beyond 300 that one crash would never have happened?
We're testing this out right now which so far seems to be working. Will update as we see more results
class ResumingServiceManager(val lifecycle: Lifecycle) : LifecycleObserver
init
lifecycle.addObserver(this)
val disposable: CompositeDisposable = CompositeDisposable()
fun startService(context: Context, intent: Intent)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
context.startService(intent)
else
Single.just(true)
.delaySubscription(300, TimeUnit.MILLISECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess =
context.startService(intent)
).addTo(disposable)
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopped()
disposable.clear()
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy()
lifecycle.removeObserver(this)
In onCreate()
initialize it and then anytime you want to start a service in onResume just call resumingServiceManager.startService(this, intent)
It's lifecycle aware so it will clear the disposable if it pauses cancelling the onSuccess from triggering when it might be on the way to the background with an immediate open/close.
1
For the Oreo+ case, would this be equivalent to something like:handler.postDelayed(context.startService(intent), 300)
and in thestopped()
, callinghandler.removeCallbacksAndMessages(null)
?
– Carmen
Feb 14 at 9:00
1
I believe that would function the same yes. Can always test it by adding some extra logging and set the time to a second or two the confirm the delay / proper removal and ability to trigger as you open/close the app repeatedly.
– Ben987654
Feb 14 at 16:00
Can anyone confirm non Rx version works posted above using handler.postDelayed
– Rinav
Jun 17 at 10:57
@rinav I would look at using googles official recommended solution at this point. I've taken out the lifecycle logic above and put their fix in the else branch instead of the Rx and it does prevent crashes. The only question is, is it completely reliable or not, which I don't know yet but we'll find out once we do a release soon. The Rx version above while it drastically reduced crashes, I still got 3-4 a month by the time I replaced every call, instead of 400-500. The good news though, is routing al the calls through this one file made it easy to swap fixes!
– Ben987654
Jun 17 at 15:53
add a comment |
UPDATE: This is working for us in Prod, but it's not 100%. I have received one crash report over the past month and a half when there would have been well over a hundred otherwise. Until this is properly fixed, this seems like our best option for now. Maybe if I raised the time beyond 300 that one crash would never have happened?
We're testing this out right now which so far seems to be working. Will update as we see more results
class ResumingServiceManager(val lifecycle: Lifecycle) : LifecycleObserver
init
lifecycle.addObserver(this)
val disposable: CompositeDisposable = CompositeDisposable()
fun startService(context: Context, intent: Intent)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
context.startService(intent)
else
Single.just(true)
.delaySubscription(300, TimeUnit.MILLISECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess =
context.startService(intent)
).addTo(disposable)
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopped()
disposable.clear()
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy()
lifecycle.removeObserver(this)
In onCreate()
initialize it and then anytime you want to start a service in onResume just call resumingServiceManager.startService(this, intent)
It's lifecycle aware so it will clear the disposable if it pauses cancelling the onSuccess from triggering when it might be on the way to the background with an immediate open/close.
1
For the Oreo+ case, would this be equivalent to something like:handler.postDelayed(context.startService(intent), 300)
and in thestopped()
, callinghandler.removeCallbacksAndMessages(null)
?
– Carmen
Feb 14 at 9:00
1
I believe that would function the same yes. Can always test it by adding some extra logging and set the time to a second or two the confirm the delay / proper removal and ability to trigger as you open/close the app repeatedly.
– Ben987654
Feb 14 at 16:00
Can anyone confirm non Rx version works posted above using handler.postDelayed
– Rinav
Jun 17 at 10:57
@rinav I would look at using googles official recommended solution at this point. I've taken out the lifecycle logic above and put their fix in the else branch instead of the Rx and it does prevent crashes. The only question is, is it completely reliable or not, which I don't know yet but we'll find out once we do a release soon. The Rx version above while it drastically reduced crashes, I still got 3-4 a month by the time I replaced every call, instead of 400-500. The good news though, is routing al the calls through this one file made it easy to swap fixes!
– Ben987654
Jun 17 at 15:53
add a comment |
UPDATE: This is working for us in Prod, but it's not 100%. I have received one crash report over the past month and a half when there would have been well over a hundred otherwise. Until this is properly fixed, this seems like our best option for now. Maybe if I raised the time beyond 300 that one crash would never have happened?
We're testing this out right now which so far seems to be working. Will update as we see more results
class ResumingServiceManager(val lifecycle: Lifecycle) : LifecycleObserver
init
lifecycle.addObserver(this)
val disposable: CompositeDisposable = CompositeDisposable()
fun startService(context: Context, intent: Intent)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
context.startService(intent)
else
Single.just(true)
.delaySubscription(300, TimeUnit.MILLISECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess =
context.startService(intent)
).addTo(disposable)
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopped()
disposable.clear()
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy()
lifecycle.removeObserver(this)
In onCreate()
initialize it and then anytime you want to start a service in onResume just call resumingServiceManager.startService(this, intent)
It's lifecycle aware so it will clear the disposable if it pauses cancelling the onSuccess from triggering when it might be on the way to the background with an immediate open/close.
UPDATE: This is working for us in Prod, but it's not 100%. I have received one crash report over the past month and a half when there would have been well over a hundred otherwise. Until this is properly fixed, this seems like our best option for now. Maybe if I raised the time beyond 300 that one crash would never have happened?
We're testing this out right now which so far seems to be working. Will update as we see more results
class ResumingServiceManager(val lifecycle: Lifecycle) : LifecycleObserver
init
lifecycle.addObserver(this)
val disposable: CompositeDisposable = CompositeDisposable()
fun startService(context: Context, intent: Intent)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
context.startService(intent)
else
Single.just(true)
.delaySubscription(300, TimeUnit.MILLISECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess =
context.startService(intent)
).addTo(disposable)
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopped()
disposable.clear()
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy()
lifecycle.removeObserver(this)
In onCreate()
initialize it and then anytime you want to start a service in onResume just call resumingServiceManager.startService(this, intent)
It's lifecycle aware so it will clear the disposable if it pauses cancelling the onSuccess from triggering when it might be on the way to the background with an immediate open/close.
edited Feb 4 at 18:14
answered Nov 10 '18 at 0:49
Ben987654Ben987654
95710 silver badges26 bronze badges
95710 silver badges26 bronze badges
1
For the Oreo+ case, would this be equivalent to something like:handler.postDelayed(context.startService(intent), 300)
and in thestopped()
, callinghandler.removeCallbacksAndMessages(null)
?
– Carmen
Feb 14 at 9:00
1
I believe that would function the same yes. Can always test it by adding some extra logging and set the time to a second or two the confirm the delay / proper removal and ability to trigger as you open/close the app repeatedly.
– Ben987654
Feb 14 at 16:00
Can anyone confirm non Rx version works posted above using handler.postDelayed
– Rinav
Jun 17 at 10:57
@rinav I would look at using googles official recommended solution at this point. I've taken out the lifecycle logic above and put their fix in the else branch instead of the Rx and it does prevent crashes. The only question is, is it completely reliable or not, which I don't know yet but we'll find out once we do a release soon. The Rx version above while it drastically reduced crashes, I still got 3-4 a month by the time I replaced every call, instead of 400-500. The good news though, is routing al the calls through this one file made it easy to swap fixes!
– Ben987654
Jun 17 at 15:53
add a comment |
1
For the Oreo+ case, would this be equivalent to something like:handler.postDelayed(context.startService(intent), 300)
and in thestopped()
, callinghandler.removeCallbacksAndMessages(null)
?
– Carmen
Feb 14 at 9:00
1
I believe that would function the same yes. Can always test it by adding some extra logging and set the time to a second or two the confirm the delay / proper removal and ability to trigger as you open/close the app repeatedly.
– Ben987654
Feb 14 at 16:00
Can anyone confirm non Rx version works posted above using handler.postDelayed
– Rinav
Jun 17 at 10:57
@rinav I would look at using googles official recommended solution at this point. I've taken out the lifecycle logic above and put their fix in the else branch instead of the Rx and it does prevent crashes. The only question is, is it completely reliable or not, which I don't know yet but we'll find out once we do a release soon. The Rx version above while it drastically reduced crashes, I still got 3-4 a month by the time I replaced every call, instead of 400-500. The good news though, is routing al the calls through this one file made it easy to swap fixes!
– Ben987654
Jun 17 at 15:53
1
1
For the Oreo+ case, would this be equivalent to something like:
handler.postDelayed(context.startService(intent), 300)
and in the stopped()
, calling handler.removeCallbacksAndMessages(null)
?– Carmen
Feb 14 at 9:00
For the Oreo+ case, would this be equivalent to something like:
handler.postDelayed(context.startService(intent), 300)
and in the stopped()
, calling handler.removeCallbacksAndMessages(null)
?– Carmen
Feb 14 at 9:00
1
1
I believe that would function the same yes. Can always test it by adding some extra logging and set the time to a second or two the confirm the delay / proper removal and ability to trigger as you open/close the app repeatedly.
– Ben987654
Feb 14 at 16:00
I believe that would function the same yes. Can always test it by adding some extra logging and set the time to a second or two the confirm the delay / proper removal and ability to trigger as you open/close the app repeatedly.
– Ben987654
Feb 14 at 16:00
Can anyone confirm non Rx version works posted above using handler.postDelayed
– Rinav
Jun 17 at 10:57
Can anyone confirm non Rx version works posted above using handler.postDelayed
– Rinav
Jun 17 at 10:57
@rinav I would look at using googles official recommended solution at this point. I've taken out the lifecycle logic above and put their fix in the else branch instead of the Rx and it does prevent crashes. The only question is, is it completely reliable or not, which I don't know yet but we'll find out once we do a release soon. The Rx version above while it drastically reduced crashes, I still got 3-4 a month by the time I replaced every call, instead of 400-500. The good news though, is routing al the calls through this one file made it easy to swap fixes!
– Ben987654
Jun 17 at 15:53
@rinav I would look at using googles official recommended solution at this point. I've taken out the lifecycle logic above and put their fix in the else branch instead of the Rx and it does prevent crashes. The only question is, is it completely reliable or not, which I don't know yet but we'll find out once we do a release soon. The Rx version above while it drastically reduced crashes, I still got 3-4 a month by the time I replaced every call, instead of 400-500. The good news though, is routing al the calls through this one file made it easy to swap fixes!
– Ben987654
Jun 17 at 15:53
add a comment |
our team faced with the same problem. My calendar shows 5 of April, 2019, but problem still reproduce on my Samsung Galaxy S9+, android 9.0 (One UI)
We start service in onResume and unbind onPause.
How to reproduce
Just lock your device, when activity with this logic is on the screen, and don't touch 10-15 minutes. After unlock screen, app will crash.
How to fix
We found solution that actually work. Since android 8+, start your service in android.os.Handler.post(...)
Example (Kotlin):
override fun onResume()
super.onResume()
Handler().post
val serviceIntent = Intent(activity, SomeService::class.java)
activity?.startService(serviceIntent)
activity?.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)
Good luck!
add a comment |
our team faced with the same problem. My calendar shows 5 of April, 2019, but problem still reproduce on my Samsung Galaxy S9+, android 9.0 (One UI)
We start service in onResume and unbind onPause.
How to reproduce
Just lock your device, when activity with this logic is on the screen, and don't touch 10-15 minutes. After unlock screen, app will crash.
How to fix
We found solution that actually work. Since android 8+, start your service in android.os.Handler.post(...)
Example (Kotlin):
override fun onResume()
super.onResume()
Handler().post
val serviceIntent = Intent(activity, SomeService::class.java)
activity?.startService(serviceIntent)
activity?.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)
Good luck!
add a comment |
our team faced with the same problem. My calendar shows 5 of April, 2019, but problem still reproduce on my Samsung Galaxy S9+, android 9.0 (One UI)
We start service in onResume and unbind onPause.
How to reproduce
Just lock your device, when activity with this logic is on the screen, and don't touch 10-15 minutes. After unlock screen, app will crash.
How to fix
We found solution that actually work. Since android 8+, start your service in android.os.Handler.post(...)
Example (Kotlin):
override fun onResume()
super.onResume()
Handler().post
val serviceIntent = Intent(activity, SomeService::class.java)
activity?.startService(serviceIntent)
activity?.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)
Good luck!
our team faced with the same problem. My calendar shows 5 of April, 2019, but problem still reproduce on my Samsung Galaxy S9+, android 9.0 (One UI)
We start service in onResume and unbind onPause.
How to reproduce
Just lock your device, when activity with this logic is on the screen, and don't touch 10-15 minutes. After unlock screen, app will crash.
How to fix
We found solution that actually work. Since android 8+, start your service in android.os.Handler.post(...)
Example (Kotlin):
override fun onResume()
super.onResume()
Handler().post
val serviceIntent = Intent(activity, SomeService::class.java)
activity?.startService(serviceIntent)
activity?.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)
Good luck!
answered Apr 5 at 12:50
Рома БогданРома Богдан
1511 silver badge3 bronze badges
1511 silver badge3 bronze badges
add a comment |
add a comment |
This has been marked as 'fixed' in the Android Issue Tracker:
- https://issuetracker.google.com/issues/110237673
- https://issuetracker.google.com/issues/113122354
Presumably the fix will be released in one of the Android Q releases.
According to the Googler who closed the issue,
There is a workaround to avoid application crash. Applications can get the process state in
Activity.onResume()
by callingActivityManager.getRunningAppProcesses()
and avoid starting Service if the importance level is lower thanActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
add a comment |
This has been marked as 'fixed' in the Android Issue Tracker:
- https://issuetracker.google.com/issues/110237673
- https://issuetracker.google.com/issues/113122354
Presumably the fix will be released in one of the Android Q releases.
According to the Googler who closed the issue,
There is a workaround to avoid application crash. Applications can get the process state in
Activity.onResume()
by callingActivityManager.getRunningAppProcesses()
and avoid starting Service if the importance level is lower thanActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
add a comment |
This has been marked as 'fixed' in the Android Issue Tracker:
- https://issuetracker.google.com/issues/110237673
- https://issuetracker.google.com/issues/113122354
Presumably the fix will be released in one of the Android Q releases.
According to the Googler who closed the issue,
There is a workaround to avoid application crash. Applications can get the process state in
Activity.onResume()
by callingActivityManager.getRunningAppProcesses()
and avoid starting Service if the importance level is lower thanActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
This has been marked as 'fixed' in the Android Issue Tracker:
- https://issuetracker.google.com/issues/110237673
- https://issuetracker.google.com/issues/113122354
Presumably the fix will be released in one of the Android Q releases.
According to the Googler who closed the issue,
There is a workaround to avoid application crash. Applications can get the process state in
Activity.onResume()
by callingActivityManager.getRunningAppProcesses()
and avoid starting Service if the importance level is lower thanActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
answered Mar 26 at 10:05
Tim MalseedTim Malseed
3,7774 gold badges35 silver badges55 bronze badges
3,7774 gold badges35 silver badges55 bronze badges
add a comment |
add a comment |
They fixed the crash (in some future release, not sure which exactly) and provided a workaround for now:
https://issuetracker.google.com/issues/110237673#comment9
add a comment |
They fixed the crash (in some future release, not sure which exactly) and provided a workaround for now:
https://issuetracker.google.com/issues/110237673#comment9
add a comment |
They fixed the crash (in some future release, not sure which exactly) and provided a workaround for now:
https://issuetracker.google.com/issues/110237673#comment9
They fixed the crash (in some future release, not sure which exactly) and provided a workaround for now:
https://issuetracker.google.com/issues/110237673#comment9
answered Mar 26 at 10:04
DimezisDimezis
1,2206 silver badges20 bronze badges
1,2206 silver badges20 bronze badges
add a comment |
add a comment |
Maybe android.arch.lifecycle could be used as a workaround for this Android 9 bug?
public class MyActivity extends Activity implements LifecycleObserver
protected void onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
startService(intent);
else
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
else
startService(intent);
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onEnterForeground()
startService(intent);
ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
I've found it here.
1
The lifecycle observer is just responding to events triggered by the activity. So this all really amounts to just moving thestartService()
call toonStart()
. As you say though, this might be a handy workaround.
– Tim Malseed
Oct 21 '18 at 23:36
1
Unfortunately I am seeing the same exception withLifecycle.State.STARTED
. I am going to tryLifecycle.State.RESUMED
and@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
which is called afteronResume()
and see if that fixes the issue.
– MarcinLe
Oct 22 '18 at 18:05
Thanks for sharing this workaround. Did you have any positive results when using Lifecycle.State.RESUMED and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)?
– Renso Lohuis
Oct 29 '18 at 10:47
I updated my app to use State.RESUMED. For a few days it was working fine, so I hoped this solves the issue, but I have just noticed another crash on android 9 again :(
– MarcinLe
Oct 30 '18 at 11:41
Yeah, not surprised at all. Again, Arch Lifecycle events are just triggered by the lifecycle events of the underlying Fragment/Activity, so this is no different to just moving thestartService
call intoonStart()
oronResume()
or wherever, unless you account for very minuscule timing delays. Sounds like moving the call toonStart()
is not a workaround.
– Tim Malseed
Oct 30 '18 at 23:26
|
show 2 more comments
Maybe android.arch.lifecycle could be used as a workaround for this Android 9 bug?
public class MyActivity extends Activity implements LifecycleObserver
protected void onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
startService(intent);
else
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
else
startService(intent);
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onEnterForeground()
startService(intent);
ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
I've found it here.
1
The lifecycle observer is just responding to events triggered by the activity. So this all really amounts to just moving thestartService()
call toonStart()
. As you say though, this might be a handy workaround.
– Tim Malseed
Oct 21 '18 at 23:36
1
Unfortunately I am seeing the same exception withLifecycle.State.STARTED
. I am going to tryLifecycle.State.RESUMED
and@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
which is called afteronResume()
and see if that fixes the issue.
– MarcinLe
Oct 22 '18 at 18:05
Thanks for sharing this workaround. Did you have any positive results when using Lifecycle.State.RESUMED and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)?
– Renso Lohuis
Oct 29 '18 at 10:47
I updated my app to use State.RESUMED. For a few days it was working fine, so I hoped this solves the issue, but I have just noticed another crash on android 9 again :(
– MarcinLe
Oct 30 '18 at 11:41
Yeah, not surprised at all. Again, Arch Lifecycle events are just triggered by the lifecycle events of the underlying Fragment/Activity, so this is no different to just moving thestartService
call intoonStart()
oronResume()
or wherever, unless you account for very minuscule timing delays. Sounds like moving the call toonStart()
is not a workaround.
– Tim Malseed
Oct 30 '18 at 23:26
|
show 2 more comments
Maybe android.arch.lifecycle could be used as a workaround for this Android 9 bug?
public class MyActivity extends Activity implements LifecycleObserver
protected void onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
startService(intent);
else
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
else
startService(intent);
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onEnterForeground()
startService(intent);
ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
I've found it here.
Maybe android.arch.lifecycle could be used as a workaround for this Android 9 bug?
public class MyActivity extends Activity implements LifecycleObserver
protected void onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
startService(intent);
else
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
else
startService(intent);
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onEnterForeground()
startService(intent);
ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
I've found it here.
edited Oct 30 '18 at 11:31
Community♦
11 silver badge
11 silver badge
answered Oct 20 '18 at 17:54
MarcinLeMarcinLe
1501 silver badge6 bronze badges
1501 silver badge6 bronze badges
1
The lifecycle observer is just responding to events triggered by the activity. So this all really amounts to just moving thestartService()
call toonStart()
. As you say though, this might be a handy workaround.
– Tim Malseed
Oct 21 '18 at 23:36
1
Unfortunately I am seeing the same exception withLifecycle.State.STARTED
. I am going to tryLifecycle.State.RESUMED
and@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
which is called afteronResume()
and see if that fixes the issue.
– MarcinLe
Oct 22 '18 at 18:05
Thanks for sharing this workaround. Did you have any positive results when using Lifecycle.State.RESUMED and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)?
– Renso Lohuis
Oct 29 '18 at 10:47
I updated my app to use State.RESUMED. For a few days it was working fine, so I hoped this solves the issue, but I have just noticed another crash on android 9 again :(
– MarcinLe
Oct 30 '18 at 11:41
Yeah, not surprised at all. Again, Arch Lifecycle events are just triggered by the lifecycle events of the underlying Fragment/Activity, so this is no different to just moving thestartService
call intoonStart()
oronResume()
or wherever, unless you account for very minuscule timing delays. Sounds like moving the call toonStart()
is not a workaround.
– Tim Malseed
Oct 30 '18 at 23:26
|
show 2 more comments
1
The lifecycle observer is just responding to events triggered by the activity. So this all really amounts to just moving thestartService()
call toonStart()
. As you say though, this might be a handy workaround.
– Tim Malseed
Oct 21 '18 at 23:36
1
Unfortunately I am seeing the same exception withLifecycle.State.STARTED
. I am going to tryLifecycle.State.RESUMED
and@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
which is called afteronResume()
and see if that fixes the issue.
– MarcinLe
Oct 22 '18 at 18:05
Thanks for sharing this workaround. Did you have any positive results when using Lifecycle.State.RESUMED and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)?
– Renso Lohuis
Oct 29 '18 at 10:47
I updated my app to use State.RESUMED. For a few days it was working fine, so I hoped this solves the issue, but I have just noticed another crash on android 9 again :(
– MarcinLe
Oct 30 '18 at 11:41
Yeah, not surprised at all. Again, Arch Lifecycle events are just triggered by the lifecycle events of the underlying Fragment/Activity, so this is no different to just moving thestartService
call intoonStart()
oronResume()
or wherever, unless you account for very minuscule timing delays. Sounds like moving the call toonStart()
is not a workaround.
– Tim Malseed
Oct 30 '18 at 23:26
1
1
The lifecycle observer is just responding to events triggered by the activity. So this all really amounts to just moving the
startService()
call to onStart()
. As you say though, this might be a handy workaround.– Tim Malseed
Oct 21 '18 at 23:36
The lifecycle observer is just responding to events triggered by the activity. So this all really amounts to just moving the
startService()
call to onStart()
. As you say though, this might be a handy workaround.– Tim Malseed
Oct 21 '18 at 23:36
1
1
Unfortunately I am seeing the same exception with
Lifecycle.State.STARTED
. I am going to try Lifecycle.State.RESUMED
and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
which is called after onResume()
and see if that fixes the issue.– MarcinLe
Oct 22 '18 at 18:05
Unfortunately I am seeing the same exception with
Lifecycle.State.STARTED
. I am going to try Lifecycle.State.RESUMED
and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
which is called after onResume()
and see if that fixes the issue.– MarcinLe
Oct 22 '18 at 18:05
Thanks for sharing this workaround. Did you have any positive results when using Lifecycle.State.RESUMED and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)?
– Renso Lohuis
Oct 29 '18 at 10:47
Thanks for sharing this workaround. Did you have any positive results when using Lifecycle.State.RESUMED and @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)?
– Renso Lohuis
Oct 29 '18 at 10:47
I updated my app to use State.RESUMED. For a few days it was working fine, so I hoped this solves the issue, but I have just noticed another crash on android 9 again :(
– MarcinLe
Oct 30 '18 at 11:41
I updated my app to use State.RESUMED. For a few days it was working fine, so I hoped this solves the issue, but I have just noticed another crash on android 9 again :(
– MarcinLe
Oct 30 '18 at 11:41
Yeah, not surprised at all. Again, Arch Lifecycle events are just triggered by the lifecycle events of the underlying Fragment/Activity, so this is no different to just moving the
startService
call into onStart()
or onResume()
or wherever, unless you account for very minuscule timing delays. Sounds like moving the call to onStart()
is not a workaround.– Tim Malseed
Oct 30 '18 at 23:26
Yeah, not surprised at all. Again, Arch Lifecycle events are just triggered by the lifecycle events of the underlying Fragment/Activity, so this is no different to just moving the
startService
call into onStart()
or onResume()
or wherever, unless you account for very minuscule timing delays. Sounds like moving the call to onStart()
is not a workaround.– Tim Malseed
Oct 30 '18 at 23:26
|
show 2 more comments
protected by Community♦ Nov 8 '18 at 14:05
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
7
I don't have a good answer for you but I can confirm we're seeing this as well. We've never reproduced it in-house, but similarly we're seeing it when starting the service in onResume(). I suspect this is a bug in Android P.
– kcoppock
Aug 25 '18 at 2:51
8
OK, glad it's not just me. This issue has been reported: issuetracker.google.com/issues/113122354
– Tim Malseed
Aug 25 '18 at 2:59
try in manifiest add process name in service tag
– Ashvin solanki
Aug 25 '18 at 5:03
3
Also, Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. From developer.android.com/about/versions/pie/android-9.0-changes-28
– Access Denied
Oct 8 '18 at 9:13
2
@iaindownie Yes, it appears to have worked, it's been in prod for a short while now, and new instances that I hadn't fixed (didn't know about at the time) started showing up since it made it further along.
– Ben987654
Jan 7 at 17:13