how to put application on sleep mode - android

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.

Related

Does the CPU stay awake when the device is charging?

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).

White listed android app in doze mode

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.

How not to loose phone charge when connected with USB for prolonged period of time

I have a Android device which I keep connected with my Jenkins server(through USB) which basically run my automation every day at a specified time but every now and then I keep running into problems where device starts to loose charge overnight or when I am remotely accessing(Vysor) the device and run some automation tests on it , it starts to loose and eventually die down. I am looking for suggestion to keep my device up and running 24/7 without loosing charge when I am running my automation cases.
I had tried all of the options you mentioned. I am now using powered USB hub which keeps it charging on power 24/7.
There are some things that you can do to keep your Android device from going to sleep.
enable developer mode (I assume you have done that already since you
are running automatic tests).
on settings->developer menu turn USB
debugging on (again I assume this is already done)
on settings->developer menu turn on Stay Awake so that the display
never goes to sleep when the device is charging.
It may also be that the issue is with your device. In that case I suggest you try the same thing with some mainstream Android phone and see if the behavior differs.

Android Doze mode

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.

When does android device go to sleep mode?

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

Categories

Resources