The documentation mentions that 6.0+ Android devices will not enter doze mode while charging, but what about regular sleep mode? I could not find anything that indicates that the CPU will turn off when it is plugged to a charger.
From some testing I've done it seems that the device is fine without the CPU WakeLock when running background processes, but I want to make sure; because it could be a case of another app holding a WakeLock.
Android sleeps when no wake-lock is active no matter what thread or process you have to execute even when charging and some modified os like miui and other Chinese roms have aggressive cpu sleep, so it's better use wake lock or Other alternative check for more info here [https://developer.android.com/training/scheduling/wakelock].
Doze is more aggressive about preserving your battery life by putting your phone in a low-power.
In this state apps background processes execute in batches, this might have issues executing in background.
And from version 8.0 Google restrict background tasks even more
From my testing with battery historian, CPU will not sleep if phone is charging.
Same for doze, it will not enter doze if phone is charging (I have a patch to force doze even while charging).
Related
I faced the Android scan stopping issue in background when I am developing my COVID contact tracing app for my company. Here is what I have tried:
Add foreground service
Disable all the battery related optimization options in the phones
Enable the application running in background
Tesging devices:Galaxy S20 and Xperia with Android 10, Huawei with Android 8.
The scan stops immediately when going to background if you don't disable those battery optimization settings and application background update. After you disabled those settings, the scan can run about a couple of minutes(~5 minutes), then still stops. From the blog of David:http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8, it seems that it is impossible to scan continuesly in background, because the JobScheduler will restart every 15 minutes while each scan lasts ~10 minutes at most. Is this the reality, or this is the best solution that I can scan 10 minutes at every 15 mins cycle?
Background beacon detections are tricky to implement because many small issues can trip you up, and the specific issues vary by Android version, Android manufacturer and sometimes model. While Android 8+ restricts background ranging to every 15 mins using the Job Scheduler, if you add a Foreground Service, you can unlock unlimited background ranging.
A few tips:
Focus first on the Galaxy S20 as Samsung behaviors are better documented and closer to vanilla Android. (Ideally you would test on a Pixel device first.) Only once it works on Samsung, move on to the others.
Using the Android Beacon Library reference app configured with the built-in foreground service, I have seen detections of a standard iBeacon or AltBeacon continue on a Galaxy S10 indefinitely in the background, even with a 1.1 second scan period. See if you can reproduce the same.
Be careful of Doze mode. If the CPU is put to sleep due to the phone being motionless with the screen off and not charging, your detections will stop. You can defeat Doze mode with wake locks, but it has a punishing impact on battery usage. You are better off accepting its limitations and keeping your phone in motion periodically during testing. If you want to get logs to see what is happening, use ADB commands to disable charging when connected via USB or learn to use ADB over WiFi.
I am trying to make a GPS tracker app.
I don't intent to distribute this app in play store. So, I got full control of the device(and can do manually whitelist).
I would like to know if:
Would doze mode be activated if charger is always connected to
phone?
Would sudden jerk or vibration to phone disables doze mode
after a prolonged inactivity? For example,consider phone placed
inside a truck and would that vibration (from engine start or
travelling) be sufficient to get the device from doze to normal
mode?
Will job scheduler service work in doze mode with scheduled
app manually whitelisted (including GPS and network)?
Would phone call to doze mode device changes its state to normal mode?
Would sensors like gyro, accelerometer work in doze mode?
Question 1:
Would doze mode be activated if charger is always connected to phone?
No.
As soon as the user wakes the device by moving it, turning on the
screen, or connecting a charger, the system exits Doze and all apps
return to normal activity.
Question 2:
Would sudden jerk or vibration to phone disables doze mode after a
prolonged inactivity? For example,consider phone placed inside a truck and would that vibration (from engine start or travelling) be sufficient to get
the device from doze to normal mode?
Prior to Android N, Yes. From Android N, no.
Android 7.0 brings further enhancements to Doze by applying a subset
of CPU and network restrictions while the device is unplugged with the
screen turned off, but not necessarily stationary, for example, when a
handset is traveling in a user’s pocket.
Question 3
Will job scheduler service work in doze mode with scheduled app
manually whitelisted (including GPS and network)?
No.
An app that is whitelisted can use the network and hold partial wake
locks during Doze and App Standby. However, other restrictions still
apply to the whitelisted app, just as they do to other apps. For
example, the whitelisted app’s jobs and syncs are deferred (on API
level 23 and below), and its regular AlarmManager alarms do not fire.
Question 4
Would phone call to doze mode device changes its state to normal mode?
Yes. Due to same reason as Question 1.
Question 5
Would sensors like gyro, accelerometer work in doze mode?
Might not work in Doze mode
For more details on Optimizations in Doze mode, you can refer this document.
Documentation says:
If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services. It also prevents apps from accessing the network and defers their jobs, syncs, and standard alarms.
What exactly stationary mean? Does phone go into Doze if it is stationary in a car that is moving? I'm developing an app that send location update to a server and if phone go into Doze mode while moving in car that can't be done.
Stationary means literally stationary, no gyro activity, screen off, not connected to charger. As far as the car is moving, the phone will not enter Doze mode.
Android developers: Optimizing for Doze and App Standby
It should be noted that Android N has extended doze mode further. Here the device doesn't need to be 'absolutely stationary' for a 'lighter' version of doze - that allows wake locks but restricts network access - to kick in.
The principal is that to truly take advantage of this, android needs to conserve battery if the phone is in a user's pocket where it is 'unused' but may not be stationary.
The original Marshmallow 'deep-doze' is still very much present in N so once the phone is stationary for a good amount of time [screen off + not being charged are already met criteria] it will go into the original doze where wifi scans and gps scans [location updates], wake locks, job schedulers, sync adapters, and network access is restricted.
To answer your question for location updates, android has a fine motion detecting sensor which ensures that the device is stationary w.r.t earth before diving into the deeper version of doze. So for this particular use case you should be good to go but you should still use the newly introduced Job scheduler api for batched updates. I am sure fetching location is one aspect, you want to upload it to your servers as well. Network access is restricted in both versions of doze modes.
references
https://plus.google.com/+AndroidDevelopers/posts/gwA68kaVJQ8
https://www.youtube.com/watch?v=VC2Hlb22mZM
In case of Android M doze mode will not get kicked in if the device is in moving car.
But in Android N, a light doze mode has been introduced that does not require the device to be completely stationary or wait as long to activate.It starts shortly after both the screen is off and the device is not charging, waiting only a couple minutes before applying the restrictions just to make sure the user has stopped using their phone.
In case,phone is Android N it may go into light doze mode.
My question is fairly simple. I would like to know what it is the best practice for my app so that it can be "Doze-proof". This is getting even more relevant as Android N will be applying Doze in more case scenario.
While reading Doze Documentation there is a part mentioning network access :
In Doze mode, the system attempts to conserve battery by restricting
apps' access to network and CPU-intensive services. It also prevents
apps from accessing the network and defers their jobs, syncs, and
standard alarms.
I believe Buetooth falls under Network Access, is that correct?
Since I do not own a Marshmallow (or Android N) enabled-device and since Emulator does not allow Bluetooth interaction I cannot really test my app behavior in Doze Mode.
Will Doze Mode kill any ongoing Bluetooth connexion? Does the same apply either for Bluetooth Classic and LE ? What about headsets with Bluetooth A2DP ?
My app must maintain this connection or else the core functionality will be broken.
Surely there is some kind of exceptions for such case where your device needs to be connected to a Bluetooth remote device for as long the user whishes too.
I know there exists a Doze Whitelist but it would seem it might not, in some cases, let the app behave as it would on a device below API 23.
Thank you for your help!
Looks like doze doesn't have any restriction on bluetooth per se but to do anything meaningful, you would need to keep the cpu on and wake locks are ignored in deeper doze.
If it's a core functionality of your app, you can try to ask users to whitelist your app. This can grant partial wake locks and keep the cpu on.
Foreground services are also exempted from any doze restrictions so that's another transparent way.
I was searching SO and web but i couldn't find sufficient answer for my particular question. Ok here we go:
Every user can modify screen timeout on android. (when screen turns off when its idle for __ seconds/minutes - users choice ). But user can modify ONLY screen timeout and not when device goes actually to sleep. I found here what happens when device goes to sleep: (Really nice answer by CommonsWare) Android Sleep/Standby Mode
Ok my question is simple. How do i know when my phone will go to sleep? Will it go to sleep immediately after turning screen off? Will it stay "awake" a while after screen was turned off? And most important: Does sleep mode in particular device varies from brand device ( how manufacturer implement this ) or it depends on ROM version (For instance, all ICS powered devices will go to sleep X seconds after screen was turned off )?
You can never really be sure if the device has gone to sleep. At best, I can give you a list of things that would make sure that the device hasn't gone to sleep:
Screen being on - Requires CPU and GPU, and hence RAM, to stay active
Any app holding a wakelock - Even the lowest wakelocks keep the CPU on, though the screen may be switched off
Apart from this, there is no guarantee that the device will go to sleep n seconds after the screen is turned off, even if no wakelocks are being held. This is upto the OEM, and they can alter this.
Keep in mind that Android devices have an option to keep the device unlocked for a certain amount of time after the screen has gone to sleep.
There is small difference in power consumption when you compare a phone with laptops.
Laptops most of the times runs on direct power through charger. (Mostly we carry charge with laptop).
Mobiles mostly run on battery power. (Rarely we carry charger with us).
By keeping this in mind, android has designed in a such a way that, phone will not consume battery power if user is not interacting with the phone. This is to increase battery back up time for user.
That's why when LCD screen power is off (that means user is not interacting with phone), with in fraction of seconds CPU will also go to a mode (sleep mode) where it does minimal work and saves battery power. When CPU is in sleep mode, it accepts commands only from RIL (radio interface layer) which is basically SMS and call functions, and alarms. Other than this CPU will stop executing other applications functions.
Note : If any application wants CPU time for its emergency work when CPU is in sleep mode, then it can request CPU time by using WAKE LOCKS.
For eg : MP3 application has to keep playing songs in its service, even though user has turned off LCD screen. That means MP3 application's service has requested CPU time by using WAKE LOCKS.
Your question : How do i know when my phone will go to sleep?
- Ans : Simple it goes sleep mode when LCD power is off, with in fraction of seconds.
Your question : Does sleep mode in particular device varies from brand device
- Ans : Since android is open source, any OEM can take driver layer , which is GPU license and can modify the power manager drive to behave differently. All these sleep modes and wake locks are finally under control of Power manager driver of Linux kernel layer.
for testing sleep mode/doze in Android 6.0 you should use next commands:
$ adb shell dumpsys battery unplug
$ adb shell dumpsys deviceidle step
more info see here: Optimizing for Doze and App Standby