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
Related
I am developing a Fitness Application as part of my Bachelor Thesis, and want to keep track of step counts even when the application is completely closed. For this I am currently starting a service that utilises the built in Sensors "Step Counter" and "Step Detector". After some testing I found out that sometimes my Service gets killed and no longer keeps track of the steps taken. I left the phone on my desk overnight and walked around in the morning then I opened the application and the steps I took in the morning were not tracked, whereas when I close the application and immediatly start walking the tracking of steps still works.
Is there a way to make sure that my Service does not get killed?
Would the use of a Foreground Service solve my issue and are there any alternatives to using a foreground service?
Foreground Service is the only way if you want to assure that the service will not be killed.
The reason for this is that the foreground service always shows a notification to the user and can be killed by the user if he wants to, this is especially important if you want to know for sure what runs on your device.
All previous methods of making permanent running services are deprecated starting from android 9, when a new privacy policy was introduced.
Basically you need to keep service running in the background,
Here is the workaround to achieve this
https://stackoverflow.com/a/58162451/7579041
above link is useful for Stock ROM & Custom ROM Devices like OnePlus, OPPO, VIVO, etc
I hope this will help you out
I have an app that listens for notifications via NotificationListenerService. My app survives both the Doze mode and app stand by on almost any phone (if properly set-up e.g. exception or a foreground service).
Then come the Huawei and Xiaomi with their own modifications of Android to "improve battery life". These can mess with background running apps so badly that after a week or two the app must be reinstalled. Even clearing data will not "unfreeze" the app. Even exceptions to their own "optimizations" does not reliably fix this. Only completely disabling all their optimizations e.g. enabling "high performance mode" might fix this. But users do not want to disable these options.
How do you deal with these cases? And how can we stop manufacturers to stop messing with core Android? Not to mention the funny ANRs & crashes in Zygote :(
Then come the Huawei and Xiaomi with their own modifications of Android to "improve battery life".
Yes Xiaomi, Huawei and a few other manufacturers have their own layer of "Battery Saver" or "Security" that kills or restricts background running apps in order to improve security/battery backup, unless user white-lists your app in the relevant device settings.
What i found out about Xiaomi is that they white-list well known apps like Facebook, Whatsapp, etc. But you cannot request Xiaomi to white-list your app.
These can mess with background running apps so badly that after a week or two the app must be reinstalled. Even clearing data will not "unfreeze" the app.
These manufacturer apps simply terminate your app process. So your app should behave in such a way that termination at any moment should not create inconsistent state.
Also, you can always show a popup to user after installation asking him/her to go and white-list your app in battery saver. In Xiaomi, the user will need to select 'Do not restrict background activity' and also enable 'Auto Start' for your app. As far as i know, there is no other solution.
Let me know if you find one ;)
I have written an app that should be running continually event if device goes to sleep mode.I have tried many things like services (with startForground also) , alarm manager and persistant="true", but when device go to sleep (by user or by device) , my app is terminated.and even PowerManager does not work too.
There is an option in device setting that is 'Keep running after screen off' (in huawei device : setting > app > my app options > battery > Keep running after screen off). So when this option is active , the app is running for ever.I want implement something like this in my app programitically.
I saw some apps that implemented this , but I dont know how.
So how can I do that?
Use ForgroundService.
If you check your device applications with an app manager, you can see that some of them using a sevice that is always running. even if you stop them, some of them start working again.
see more about services behavior in android developer.
and search for "keeping a service alive".
On some devices (Huawei, Xiaomi, LG) there is a special setting that contains a list of apps that are allowed to run in the background. It sounds like you are talking about trying to accomplish the same thing. This cannot be done on those devices. On those devices, unless your app is added to the list of apps that are allowed to run in the background ("Protected Apps" on Huawei), the Android framework will not restart your app if it is shutdown. There is no way for you to programatically add your app to this list, and there is no way for you to work around this feature.
Certain apps need to keep the screen turned on, such as games or movie apps. The best way to do this is to use the FLAG_KEEP_SCREEN_ON in your activity (and only in an activity, never in a service or other app component).
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
I've developed an app which receives push notifications from Parse. The app is designed to run in background so that notifications can be received even when app is closed.
But we are facing challenge with Huawei's devices due to its custom "power savings" feature which detects the app as power intensive and hence clears it from memory when screen is locked.
Are there any known methods to prevent the app from being restricted or to add it to "Protected Apps" list upon installation?
Note: We begun with GCM notificatoins, but after learning that GCM's notification deliverability is flaky, we moved on to Parse.com's PPNS and then this new roadblock.
We cant do much except detect the device,if device is huawei warn the user about propably issue if the desired app wont be added to protected apps hence this is the only way to do what we need.No other ways available and i reckon it will never be!
I have an android app which records voice using a service - and a thread inside the service(obviously the app can record while in background..)
The app will be affected by the new Doze app state?
https://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases
I don't have a phone with 6.0 yet and the simulator cannot record voice in general...
If your service is running in the foreground (with an associated notification) when the device enters Doze mode, it should not be affected according to a comment by Dianne Hackborn to this post. See a documented experience that seems to prove this behavior here.
On the other hand, tests show, that access to certain sensors like GPS are restricted in Doze mode, so this might also apply to the microphone.
Since Doze mode is poorly documented up to now, unfortunately at this point you probably do not get around running your own tests on a physical device.
Yes, every app can be "killed" by Doze. If your service runs in foreground you can avoid App Standby however. Remember that asking to the user to put the app in the whitelist it's prohibited from Google Terms of services, so you can't do it. If you want to do something like that you need to add a permission to your manifest and with cross fingers hope in the Google review of your app.