My player app uses a foreground service for playback and yet on some devices playback is killed by the device until the app is excluded from the device Optimized apps list.
It's as if the device has a list of white listed app that won't be killed because this doesn't see to happen for big apps like Spotify, Google Music, ... and yet small player apps get killed.
This doesn't seem to be a code issue. I just want to make sure I'm not imagining this
What do you think about this
Related
I am creating an Android app (Java code) that has an audio call feature.
I managed to make it work using the webRTC framework.
I wanted to make my audio call behave like WhatsApp and Messenger, where those apps keep the audio call running even if the user stops the app from apps history, or starts using another app.
So, I decided to migrate the webRTC code from an Activity to a Foreground Service.
My Audio Call Activity handles the UI, displaying friend photo, name, call timer ...
So I had to make communication between my Activity and my Foreground Service through binding and sending android.os.Message object.
Everything works fine when using SAMSUNG devices, if I kill the app, I could return to the Call activity by clicking the foreground service's notification, and the audio call keeps working until I hang up.
My problem appeared when i used a Xiaomi device (redmi note7), if i kill the app, the audio call stops, cause my Foreground Service is restarted.
Even after enabling "Autostart" from settings.
I searched in here for a solution but all the answers i found didn't satisfy my need, cause i can't just accept my Service being restarted.
If i want to resume the audio call, i have to re-call the friend again !
So, the question is :
How to prevent Xiaomi, Huawei, Oppo ... devices from restarting Foreground Service ?
Which is the case with WhatsAPP, Messenger, Instagram and others.
It's a tricky question since it really depends on the vendor. There is a site which document and rank the vendor according to "how bad" they're handling services and processes. From the site:
...With Android 6 (Marshmallow), Google has introduced Doze mode to the base Android, in an attempt to unify battery saving across the various Android phones. Unfortunately, vendors (e.g. Xiaomi, Huawei, OnePlus or even Samsung..) did not seem to catch that ball and they all have their own battery savers, usually very poorly written, saving battery only superficially with side effects.
The solutions differs from vendor to vendor and from ROM version to another.
For example for Huawei you can overcome the issue in Huawei P20, Huawei P20 Lite, Huawei Mate 10 by Phone settings > Battery > App launch and then set your app to “Manage manually” and make sure everything is turned on. Also for reliable background processes you may need to uninstall PowerGenie (which is a power mgmt. application by Huawei). On the other hand for EMUI 9+ devices you'll have to uninstall PowerGenie via adb
I have an app with audio playback, but on some devices (mainly Huawei, Samsung, Sony) playback is stopped when the screen goes off.
Note: playback does not stop for some other apps like Spotify, Play Music
I found this answer (for Huawei devices) which suggest to ask user to enable "run in background" permission in battery optimization settings (Battery Manager > Protected Apps) but I would like to avoid asking users to enable it.
Is there some API to enable it programmatically for Huawei, Samsung, Sony or other manufacturers?
Is it possible to detect if app do not have such permission?
EDIT: playback runs in foreground service and app holds wakelock
EDIT 2: I've contacted Huawei support related to this issue. I will share more info once resolved.
Use a service with foreground priority the newer Android OS versions have a doze function to save battery.
If you already use the service in foreground probably there is something wrong with the way you use the service in your code (give more details).
Here a tutorial about Playing music in the background that could help you.
We have a music app (mp3) developed in Xamarin (Native Android App) using MediaPlayer. There will be list of songs that should be played one after the other. After around 10 minutes or so, the player stops. Once the phone is unlocked, the Player starts playing the next track. Seems the Nougat Doze is somehow intervening the Player. If I manually disable Battery Optimization for my app, then there is no issue.
Is there any fix for this problem rather than having each user to manually change the 'Batter Optimization' option. I wonder how other apps like Spotify plays songs continuously without any setting changes. Please help!
Any process that contains a running foreground service will not be considered for Doze mode. This is what apps like Spotify and Google Play Music use. Consider creating a relevant foreground service - it will be shown in the notification screen while the foreground service is running, so a dummy service will stick out.
Another option is to request the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission and then launch an ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent, which will take the user to the Battery Optimization screen for your app. This is a less-than-stellar user experience but does not require the creation of a foreground service that you may not otherwise need.
This is a simple question, seeing that there is a huge post about this on G+ (here), and lack of information on official docs (here ):
What happens to the app's services when the device goes to "doze" mode?
What does it do to background/foreground services (bound/unbound, started/not-started), with/without partial/full wakelocks?
What would you do, for example, in order to create a service that plays an audio stream while the device's screen is turned off? What if the audio stream is not from a local file, but from the network?
Seeing that there was a claim by Google developer:
Apps that have been running foreground services (with the associated
notification) are not restricted by doze.
-yet a lot of discussion after that, claiming this is not entirely true, I think it's quite confusing to know what special background-operations apps should do.
Processes which have a current running foreground service are supposed to be unaffected by Doze. Bound/unbound, started/not-started, and wakelocks do not affect this whitelisting process.
However, there is an issue on Android M devices where foreground services are not properly whitelisted when the foreground service is the in the same process as the top activity and improperly dozed.
The fix is available on AOSP and will be included in builds of Android N. It would be up to OEMs to integrate that patch into any Android M builds they produce.
I am running an accelerometer based android app that will run for a few months while phone is on and does nothing else. Some phone allow display not to go to sleep at all which allows my app run fine infinitely. The screen also has only a black display and nothing else apart from background accelerometer listener and occasional http posts. My question is if I remove the display screen while the app is running, would that stop the operating system and/or my app?
My question is if I remove the display screen while the app is running, would that stop the operating system and/or my app?
In short, it depends on your app architecture (otherwise i.e. music players would require to keep screen on to work). Depending on task you are really doing you may use Alarm Manager to periodically fire your code, or use Service.