How to keep screen state with using Wakelock? - android

I used Wakelock in my application for fire CPU on when the device went to sleep but i
don't want to turn on when screen is off, i mean, i want to keep state of screen and
just turn on cpu for my background works.
I used below code but in some of devices, in wakelock device, the screen was
turn on but as i readed about PowerManager and i realised, i had to used just
PARTIAL_WAKE_LOCK. Is that true?
before code:
wakeLock=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE,"aqs_wake_lock");
after edit:
wakeLock=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"aqs_wake_lock");

A wake lock is a mechanism to indicate that your application needs to have the device stay on
PowerManager defines various types of wakelocks
The following wake lock levels are defined, with varying effects on system power. These levels are mutually exclusive - you may only specify one of them.
As you can see from the picture partial wakelocks will continue to run irrespective of state of screen.

Related

What exactly do wake-locks prevent?

In the wake-lock training doc it says:
If you need to keep the CPU running in order to complete some work before the device goes to sleep, you can use a PowerManager system service feature called wake locks.
It was my impression that "before the device goes to sleep" referred to doze mode. However, the answer to the SO post Wakelock and doze mode states:
Holding a PARTIAL_WAKE_LOCK is insufficient to block Doze mode
So, if a wake-lock doesn't prevent doze mode, then what exactly is meant by "keep the CPU running in order to complete some work before the device goes to sleep"?
Also, in the doze standby training doc it says:
An app that is partially exempt can use the network and hold partial wake locks during Doze and App Standby.
If (for some reason) "before the devices goes to sleep" does refer to doze mode, then does this mean that wake-locks have absolutely no effect unless you are on the white list for no battery optimizations?
Specifically, I'm talking about partial wake-locks on API 31+.
what exactly is meant by "keep the CPU running in order to complete some work before the device goes to sleep"?
Android devices can power down their CPUs to reduce battery consumption. This usually happens shortly after the screen turns off.
A partial wakelock says "allow the screen to turn off but keep the CPU powered on". This is used for things like long-running audio playback (music, audiobooks, podcasts, etc.).
A full wakelock says "do not allow the screen to turn off either". This is used for things like video players, where the user's expectation is that the screen will stay on despite limited user input.

Android Wear: measuring sensors and preventing ambient mode / sleep

I have built an app for Android Wear that needs to measure the motion (accelerometer) sensors continuously for an hour or so for data collection purposes. During operation, I have had problems with the device going into ambient mode (or sleep) and slowing down (or shutting off) the sensors.
As a result, I am using the following command to lock the screen on and it seems to work. But it also seems wasteful, since I dont actually need the screen on, just the sensors running.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Allow going to sleep
CUrrently, I have the onSensorsChanged() method in the main activity. I am willing to put it in a service, but from what I understand that won't help if it is still in the main UI thread. I could put it in its own thread, but I'm not sure that that will present Ambient Mode from slowing the sensors.
Questions:
1) Is there way to prevent ambient mode? Or if detected to turn it off?
2) If the sensors are in their own service/thread, can I let the activity go to sleep and still maintain sensor collection at full rate?
3) If the sensors are in their own service/thread, can I still send data over the dataapi to the phone?
Thanks.
1) Use a wake lock. To keep the CPU awake but let the screen go dim, you can use code like the following:
PowerManager powerMgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wakeLock.acquire(duration);
where TAG is a string to indicate the component holding the wake lock, and duration is measured in milliseconds.
I assume I don't need to warn you about the adverse battery effects of keeping the device from going into ambient. An average Wear device may or may not last for the solid hour you're proposing.
2) Yes. This is kind of the definition of a Service, "an application component representing either an application's desire to perform a longer-running operation while not interacting with the user" (from https://developer.android.com/reference/android/app/Service.html).
3) Yes. I do this in a number of apps; there's no requirement that Data API calls need to be on the UI thread.

Webview behaviour wakelock android

How WebView works with wake lock?
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright
*If you hold a partial wake lock, the CPU will continue to run, regardless of any display timeouts or the state of the screen and even
after the user presses the power button. In all other wake locks, the
CPU will run, but the user can still put the device to sleep using the
power button.
In addition, you can add two more flags, which affect behavior of the screen only. These flags have no effect when combined with a PARTIAL_WAKE_LOCK.
This two FLAGS:
ACQUIRE_CAUSES_WAKEUP Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once
it turns on (e.g. from user activity). This flag will force the screen
and/or keyboard to turn on immediately, when the WakeLock is acquired.
A typical use would be for notifications which are important for the
user to see immediately.
ON_AFTER_RELEASE If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to
remain on a bit longer. This can be used to reduce flicker if you are
cycling between wake lock conditions.
Scenario: App running one webview that need wait for onPageFinished() to do the processing (And yes, I need to use wake lock, this is just the simplest scenario)
WebView doesn't aquire wakelocks by itself, it's all up to your app's code.

For which kinds of work I need to use CPU WAKE_LOCK in Sleep Mode?

For which kinds of work I need to use CPU WAKE LOCK ? For example in Sleep Mode I am using TelephonyManager getCellLocation(), should I use PARTIAL_WAKE_LOCK in this case or not? I didn't find this info in the documentation. P.S. TelephonyManager has different API implementation on different devices so for me it looks tricky. Thanks.
**Flag Value CPU Screen Keyboard**
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright
Incase you keep getting the location regularly even when the phone screen is turned off, use a partial wake lock to do so. From the above table we can see that a partial wake lock only keeps the CPU on while screen and keyboard is off(you dont require them as you just want to get location). Anyways refer to the below link for more info on WAKE LOCKS
http://developer.android.com/reference/android/os/PowerManager.html
Also refer to this question on SO, can be helpful
How can I keep my Android service running when the screen is turned off?
After some investigation looks like for all operations that can not be triggered from Manifest using system intents.

PowerManager newWakeLock.acquire() not working as expected

I want my app to keep the CPU running but turn the screen off to minimise power wastage.
Previous posts on this subject suggest the following approach:
mPm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = mPm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.WAKE_LOCK_TAG);
mWakeLock.acquire(); //keep CPU running, turn screen off
The manifest contains:
<uses-permission android:name="android.permission.WAKE_LOCK" />
In my innocence I expected that on acquiring the lock the screen would go off immediately but nothing happened.
The earlier posts I have read never seem to overcome this problem, did anyone succeed? Is there anything else I must do?
It would be more efficient in another way to use this within the onCreate method of the activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
That is a better way to use wake-locks in a efficient manner.
The wake lock does not turn off the screen. Normally, when the screen goes off, your application pauses because the CPU is put to sleep. If you have PARTIAL_WAKE_LOCK, your app will continue to run and the CPU will be active even when the screen goes off (due to auto or manual sleeping). Only use this if you really need to continue processing data after the screen is off. You mention you are concerned about minimizing power usage, acquiring a wake lock of any kind can cause waster power. It is wise to follow this warning from the PowerManager docs
Device battery life will be significantly affected by the use of this
API. Do not acquire PowerManager.WakeLocks unless you really need
them, use the minimum levels possible, and be sure to release them as
soon as possible.
So it is good idea to evaluate why you think you need a wake lock for your task. If you think it is too save power, it isn't.

Categories

Resources