codepath / android_guides

Extensive Open-Source Guides for Android Developers
guides.codepath.com
MIT License
28.29k stars 6.35k forks source link

Foreground services not working sometimes in android O and MIUI os #351

Open amitdoiphode11 opened 5 years ago

amitdoiphode11 commented 5 years ago

I have some question about location tracking and background services like (background/foreground/firebasejobdispatcher/jobshedular/workmanager).

Currently, my app is targetting android 28 version. And I am testing on Android 8.0 now. Above code is working perfectly on pre-O and post-O android version. But getting location after every 1 min.

I am creating foreground service but some devices like MI, Oneplus which stop this foreground service. Is the correct way to get locations or service will run in the background/foreground?

jobshedular/workmanager periodic time which is not lesser than 15 min.

Have anyone working on facing the same problem and resolving then please guide me.

Thank you

_Originally posted by @amitdoiphode11 in https://github.com/codepath/android_guides/issues/220#issuecomment-445515533_

hafiz013 commented 5 years ago

this is due battery optimiser from manufacture itself. u have manually allow background in setting.

rajBopche commented 3 years ago

In order to run my foreground service even after app the was killed, I enabled Settings -> Apps -> Permission -> Auto Start Permission for my App. This was the only thing which worked on MIUI

mirzaadil commented 3 years ago

If you read carefully in the end of second line

requires android.permission.FOREGROUND_SERVICE

I was also facing the same error in one of my project I resolve it by adding permission in the manifest file. So just simply add foreground permission in the manifest file.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission> Hope it's work for you.