paho.mqtt.android icon indicating copy to clipboard operation
paho.mqtt.android copied to clipboard

Crash - Not Allowed to Start Service Intent

Open NampyoJeong opened this issue 7 years ago • 21 comments

Please fill out the form below before submitting, thank you!

  • [x] Bug exists Release Version 1.1.1 (Java Repository Master Branch)
  • [ ] Bug exists in Snapshot Version 1.1.2-SNAPSHOT (Android Service Repository Master Branch)
  • [ ] Bug is just in the Sample Application.

Bug Seen on API 27(Oreo 8.1.0), Pixel

Console Log output (if available):

Exception java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.plangram.plangram.plangram/org.eclipse.paho.android.service.MqttService }: app is in background uid UidRecord{accd403 u0a307 LAST bg:+6m35s58ms idle change:cached procs:1 seq(188,188,188)} android.app.ContextImpl.startServiceCommon (ContextImpl.java:1521) android.app.ContextImpl.startService (ContextImpl.java:1477) android.content.ContextWrapper.startService (ContextWrapper.java:650) org.eclipse.paho.android.service.MqttAndroidClient.connect (MqttAndroidClient.java) <OR> org.eclipse.paho.android.service.MqttAndroidClient.connect (MqttAndroidClient.java) <OR> org.eclipse.paho.android.service.MqttAndroidClient.connect (MqttAndroidClient.java) <OR> org.eclipse.paho.android.service.MqttAndroidClient.connect (MqttAndroidClient.java)

NampyoJeong avatar Dec 19 '17 04:12 NampyoJeong

There are additional background restrictions on Oreo: https://developer.android.com/about/versions/oreo/android-8.0-changes.html#back-all

ingamedeo avatar Jan 16 '18 08:01 ingamedeo

I think that if you target older sdk versions, it doesn't happen

plentz avatar Jan 16 '18 13:01 plentz

True, just target a lower SDK for now and that should solve the issue. Long-running services are no longer allowed on Android Oreo (26) unless started in foreground mode.

On 16 Jan 2018 14:30, "Diego Plentz" [email protected] wrote:

I think that if you target older sdk versions, it doesn't happen

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipse/paho.mqtt.android/issues/274#issuecomment-357960085, or mute the thread https://github.com/notifications/unsubscribe-auth/ADz40VPoPWghkaL1pm4tvrk2wgkjvLG4ks5tLKRygaJpZM4RGcW5 .

ingamedeo avatar Jan 16 '18 13:01 ingamedeo

Any update on this?

mdakram avatar Aug 11 '18 10:08 mdakram

I don't think this will ever be fixed cos it's not really an issue

ingamedeo avatar Aug 11 '18 19:08 ingamedeo

@ingamedeo How its not an issue.

its an issue if you are bundling this in a library, as developer will use as is and app will crash.

mdakram avatar Aug 12 '18 16:08 mdakram

It's not a problem with the library. What the library used to do before is not possible anymore as Android has limited background services starting from Oreo 8.0. There is no fix Perri much other than showing a persistent notification and run the service in the foreground.

On Sun, 12 Aug 2018, 17:46 Mohammad Akram, [email protected] wrote:

@ingamedeo https://github.com/ingamedeo How its not an issue.

its an issue if you are bundling this in a library, as developer will use as is and app will crash.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eclipse/paho.mqtt.android/issues/274#issuecomment-412355592, or mute the thread https://github.com/notifications/unsubscribe-auth/ADz40aBEok3yh9j5KSCusCbZN_B4iQxDks5uQFwCgaJpZM4RGcW5 .

ingamedeo avatar Aug 12 '18 16:08 ingamedeo

Then there should be a way for developer to achieve this(showing notification), if something is part of a library which is causing crash then its problem with library.

mdakram avatar Aug 12 '18 17:08 mdakram

You can already do that. If you start the in the foreground a persistent notification will be shown and it should work as expected.

On Sun, 12 Aug 2018, 18:01 Mohammad Akram, [email protected] wrote:

Then there should be a way for developer to achieve this(showing notification), if something is part of a library which is causing crash then its problem with library.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eclipse/paho.mqtt.android/issues/274#issuecomment-412356557, or mute the thread https://github.com/notifications/unsubscribe-auth/ADz40VmhSboQKxkZCBfkGHor-oM8_GEaks5uQF9-gaJpZM4RGcW5 .

ingamedeo avatar Aug 12 '18 17:08 ingamedeo

Hi, I am using this library, so I think it is big issue when run on android 8.1. I can not edit to start service in foreground mode. I try to create a class, which extends MqttAndroidClient but I can not rewrite connect method. Please tell me how to fix it? Thanks!

nguyenvanquan7826 avatar Sep 19 '18 14:09 nguyenvanquan7826

this should be fixed by the library, starting november, only api 27 will be allowed on the Play Store, so Android 8 compatibility should be the norm

estefaniamiguel avatar Oct 17 '18 20:10 estefaniamiguel

I was running into this same issue and I managed to work around it by switching from MqttAndroidClient to MqttAsyncClient.

The library should either remove MqttAndroidClient completely or rewrite it so that it works better with Android's new background restrictions. Like stated above, no one building a new Android app can reliably use MqttAndroidClient

kachi1227 avatar Nov 29 '18 00:11 kachi1227

This is definitely a problem with the library and should be fixed.

marciogranzotto avatar Mar 14 '19 20:03 marciogranzotto

I have pulled a request for this bug, hope it can be accepted.

coderJohnZhang avatar Mar 26 '19 13:03 coderJohnZhang

I don't know, but now my app not error. I don't update lib or change taget version.

nguyenvanquan7826 avatar Jun 03 '19 04:06 nguyenvanquan7826

I don't know, but now my app not error. I don't update lib or change taget version.

I think that if you target older sdk versions, it doesn't happen

Google Play requires that apps target API level 26 or higher.

i-m-aj avatar Nov 26 '19 07:11 i-m-aj

Any updates? I'm still having this issue. Found this at Android Docs: https://developer.android.com/about/versions/oreo/android-8.0-changes.html#back-all, and this Stackoverflow post https://stackoverflow.com/a/47654126/5717211. But obviously, this would mean a change at the repo, found the specific line at MqttAndroidClient.java, line 414: Object service = myContext.startService(serviceStartIntent);, that should be changed to something like:

Object service = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? 
    myContext.startForegroundService(serviceStartIntent) : 
    myContext.startService(serviceStartIntent);

Bu I don't have a lot of experience in services, so I don't know exactly if this would work.

ArnyminerZ avatar Apr 09 '20 08:04 ArnyminerZ

Any updates? I'm still having this issue

Fatal Exception: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.psa.mym.myds/org.eclipse.paho.android.service.MqttService }: app is in background uid UidRecord{e841909 u0a96 SVC bg:+2m11s429ms idle change:uncached procs:1 proclist:8441, seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1688) at android.app.ContextImpl.startService(ContextImpl.java:1633) at android.content.ContextWrapper.startService(ContextWrapper.java:683) at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:414)

assadi-mondher avatar Jun 08 '20 08:06 assadi-mondher

From oreo, you are not allowed to start service from background. So its bug. You have to change library and use broadcast to start service

On Mon, 8 Jun 2020 at 1:57 PM, Assadi Mondher [email protected] wrote:

Any updates? I'm still having this issue

Fatal Exception: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.psa.mym.myds/org.eclipse.paho.android.service.MqttService }: app is in background uid UidRecord{e841909 u0a96 SVC bg:+2m11s429ms idle change:uncached procs:1 proclist:8441, seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1688) at android.app.ContextImpl.startService(ContextImpl.java:1633) at android.content.ContextWrapper.startService(ContextWrapper.java:683) at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:414)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipse/paho.mqtt.android/issues/274#issuecomment-640451132, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGNF7TG5PJDASWTM5ZT76LTRVSOIHANCNFSM4EIZYW4Q .

-- Thanks and Regards : Amit Jaiswal http://linkedin.com/in/amit-jaiswal-b62a1a7b (Android Engineer)

i-m-aj avatar Jun 08 '20 08:06 i-m-aj

What is exactly issue, could you explain your business logic then I might help you

On Fri, 16 Oct 2020 at 10:37 PM, Eduardo de Oliveira Freitas < [email protected]> wrote:

Is there any solution to this problem?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipse/paho.mqtt.android/issues/274#issuecomment-710237251, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGNF7TFNDYKWKC55HEPVWY3SLB4WJANCNFSM4EIZYW4Q .

-- Thanks and Regards : Amit Kumar Jaiswal http://linkedin.com/in/amit-jaiswal-b62a1a7b (SDE- I Android)

i-m-aj avatar Nov 06 '20 16:11 i-m-aj

Hi everybody,

even if its the year 2023 I would like to give my 50 cents to the topic. First some background info why I made some investigations in this area:

  • I have a production app which utilises MQTT for a messaging module. E.g. to send a notification to the user once a message arrives while the app is killed or in background or device screen is locked
  • I am using a dedicated ForegroundService (another one besides Mqtt Background Service from Paho aka MqttService.java)
  • I have faced some BackgroundStartServiceNotAllowedExceptions via Crashlytics and that's the reason why I started to look into the issue

As far as I understood it is still allowed to start a background service on android, e.g. see https://developer.android.com/guide/components/services. Starting from apiLevel 26 on there are some restrictions to start a background service. One restriction is that you can not start your background service while your app is not in the foreground. Regarding background service we can sum up that:

  • A background service is terminated once the app is terminated/killed by the user
  • Prior Android O (Android 8.0, ApiLevel 26) a background service is running forever if app is moved to background
  • On Android O (Android 8.0, ApiLevel 26) or higher a background service is stopped after a while if app is moved to background

So what I actually think is that there is nothing wrong about the paho mqtt android client (MqttAndroidClient) which starts the MqttService as a background service via Object service = myContext.startService(serviceStartIntent); on Line 415 of MqttService.java. All you have to make sure is that your app is in foreground once you start paho's MqttService and you should also start your own foreground service which displays a notification to the user telling him that there is some process/service of your app running even if you killed your app. What this foreground service actually does is that it keeps your android app process alive so that the MqttService is also alive and keeps working. In my special case I start the paho MqttService & my own foreground service once the user is logged in and stop it once the user logs out.

There are some forks of this library to work around this issue, like this one, but I think you won't need this.

One issue I have found is that the service did not call onDestroy by only using disconnect. I had to remove any registered resources/callbacks and needed to close the connection, see https://github.com/eclipse/paho.mqtt.android/issues/234

I hope this is not total nonsense and my assumptions do fit at least a bit ...

wman1980 avatar Sep 05 '23 11:09 wman1980