eclipse / paho.mqtt.android

MQTT Android
Other
2.93k stars 886 forks source link

Crash - Not Allowed to Start Service Intent #274

Open NampyoJeong opened 6 years ago

NampyoJeong commented 6 years ago

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

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)

org.eclipse.paho.android.service.MqttAndroidClient.connect (MqttAndroidClient.java) org.eclipse.paho.android.service.MqttAndroidClient.connect (MqttAndroidClient.java) org.eclipse.paho.android.service.MqttAndroidClient.connect (MqttAndroidClient.java)`
ingamedeo commented 6 years ago

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

plentz commented 6 years ago

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

ingamedeo commented 6 years ago

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" notifications@github.com 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 .

mdakram commented 6 years ago

Any update on this?

ingamedeo commented 6 years ago

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

mdakram commented 6 years ago

@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.

ingamedeo commented 6 years ago

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, notifications@github.com 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 .

mdakram commented 6 years ago

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.

ingamedeo commented 6 years ago

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, notifications@github.com 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 .

nguyenvanquan7826 commented 6 years ago

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!

estefaniamiguel commented 6 years ago

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

kachi1227 commented 5 years ago

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

marciogranzotto commented 5 years ago

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

coderJohnZhang commented 5 years ago

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

nguyenvanquan7826 commented 5 years ago

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

i-m-aj commented 4 years ago

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.

ArnyminerZ commented 4 years ago

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.

assadi-mondher commented 4 years ago

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)

i-m-aj commented 4 years ago

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 notifications@github.com 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 commented 3 years ago

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 < notifications@github.com> 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)

wman1980 commented 1 year ago

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:

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:

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 ...