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
Related
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).
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.
I have an app which is running on an android device which has no battery. It's just charging all the time and the app is running. But sometimes the device is not charging and logical to that the phone will turn off.
When it's charging again the device should start from itself and boot. The user shouldn't press the power button. That's the requirement.
Is this possible anyway?
Can I achieve this when I root the device?
Has anybody experiences with that case?
in short: No.
ADB is not running when the phone is powered off, and as #langerhans said, wake on alarm is proprietary
You could do a hardware solution where you hijack the battery pins to a power station or modified charger. (make sure you do not feed any power through usb at the same time)
It should be cheap and quite easy to build.
Generally, this is not possible. It is highly device dependant. Some devices allow alarms turning on the phone to ring as an alarm clock even if they are turned off. But this is a proprietary feature and therefore not easily accesible to custom applications.
I have basically the same problem running an Odroid device which luckily turns itself on after power loss, but if I shut it down manually, I can only turn back on with pulling the power.
The only thing I could think off would be an extra watchdog device, but I have no idea if something like that exists. Maybe you'd need to build it yourself.
In order to power saving, I want to put my android application in the sleep mode. How can I manually put my application on the sleep mode ?
In sleep mode, I know it uses less CPU and RAM. For this reason, I want .
How can I manually put my application on the sleep mode ?
Applications do not go into "sleep mode". Devices go into sleep mode.
You cannot manually put a device into sleep mode, as the user may be using it at the time. Please allow the device to fall asleep normally.
In sleep mode, I know it uses less CPU and RAM.
Applications do not use less RAM when the device is in sleep mode. The device's CPU is powered down (or put into a low-power state) during sleep mode, but, again, this is a characteristic of a device, not of an application.
I'm Developing an Android Application where it's constantly required to keep the App up an running at all time. with the phone connected to a power outlet at all time, Is it possible that if the phone's battery died to Auto-start up the app with no user intervention . *i.e to power up the phone automatically without hitting any power button and start up android since charger is connected. *
then start up my app with android.intent.action.BOOT_COMPLETED and RECEIVE_BOOT_COMPLETED permission
PS: I have tried android.intent.action.ACTION_POWER_CONNECTED but it only works if the phone is already powered up.
I have exactly same requirement as yours.
Can we plan a solution like this:
The Android device is always connected to charger's output.
Charger's power supply is connected to a custom made hardware device which controls power supply to charger.
This hardware device puts charger off until battery is getting low. i.e. 20% remaining.
Once battery is less than 20%, that device automatically switch on the charger.
The "custom made hardware" gets it input from android device itself via USB/WiFi/Bluetooth.
Just a thought. May not be practically possible.
No way doing it if you do not root your device and add reboot scripts to the programs running to show battery charging pictures when device is turned off and charging.for Samsung s2 here is an answer
Maybe you can make an additional app that waits for the BOOT_COMPLETED with its BroadcastReceiver and then check wether the power is connected and start as a new Intent your application?
Just a quick thought..
I have the same problem.
The solution that i will to implement:
1.- get out two(here is my doubt, ¿what two wires?, because i see three, i will use my multimeter for see continuity between wires when i push the button) wires from the power button phone.(when you push that button, it just connect two poles like a switch).
2.- When you have the two wires, so, we build a little circuit with a timer like a 555(integrated circuit). The purpose of the circuit will connect the two wires(from power button phone) by a 5 seconds period every 15minutes.
Obviously this circuit will be running always.