I am currently developing an app that runs a service from time to time. Currently, the service acquires a wakelock, reads some sensors and sends some information over WIFI (if any). Now what I want to know is weather a wakelock influences sensors and connectivity or not. Is it possible to do these tasks without any wakelock?
Cheers
A wake lock is essentially used to lock the device in an "awake" state, in which the CPU will be on, and the screen may or may not be on.
It is not possible to do these tasks without a wakelock if the phone is in sleep otherwise, as then the CPU is also in sleep mode. However, if the user is using the device for something else, and your app is in the background, you can do these tasks without a wakelock.
Keep in mind that almost everything you're doing is battery intensive (sensors, WiFi, wakelock) and you should not do it too often so that you don't degrade the user's battery life.
Related
Our company is developing an android application that uses network communication to send GPS signals from devices. The devices are the same and they are all work tools, so we do not have to worry about battery draining, or etc. Currently the activity has a thread, which communicates with the server. The problem is that when the device is locked and it goes to sleep, the network communication breaks.
I've tried to put a partial wake lock to the onPause method to keep the CPU on, and release the wakelock in the onResume method, but it seems not to work. Any idea how to prevent the sleep, or keep flowless communication between the client and the server?
Unfortunately, it is the new behavior, You can read here:
https://developer.android.com/about/versions/nougat/android-7.0-changes.html
See the Doze section.
When a device is on battery power, and the screen has been off for a certain time, the device enters Doze and applies the first subset of restrictions: It shuts off app network access, and defers jobs and syncs. If the device is stationary for a certain time after entering Doze, the system applies the rest of the Doze restrictions to PowerManager.WakeLock, AlarmManager alarms, GPS, and Wi-Fi scans.
It can be solved by using Foreground Service like mentioned above in comments by #egoldx.
It is surely a bad practice to try holding a wakelock, even partial, all the time.
If I have an IntentService that simply updates the SharedPreference, is a (partial) WakeLock needed?
I understand that a WakeLock keeps the CPU awake, but when is it needed?
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. Wake locks allow your application to control the power state of the host device.
Creating and holding wake locks can have a dramatic impact on the host device's battery life. Thus you should use wake locks only when strictly necessary and hold them for as short a time as possible. For example, you should never need to use a wake lock in an activity.
One legitimate case for using a wake lock might be a background service that needs to grab a wake lock to keep the CPU running to do work while the screen is off. Again, though, this practice should be minimized because of its impact on battery life.
Unfortunately, some poorly-coded, malicious, or simply buggy apps might create an abnormal amount of undesirable wakelocks. Other apps require constant Internet access in order to operate in a normal fashion - Facebook and Messenger are probably the most popular representatives. They persistently request information from the web (the so-called "polling" for new events), which is causing subsequent wakelocks.
In other cases, an update to a given app can also cause certain issues, which usually result in partial wakelocks. The latter keep your CPU constantly humming in the background, sometimes without your knowledge, and prevent your device from "going to sleep". That's a pretty substantial prerequisite for anomalous battery drain. Thus, it is advisable to regularly monitor the wakelocks on your device and see which of your apps go harsh on our system's resources.
Read more at:
What-are-wakelocks-how-they-affect-the-battery-life-of-your-Android-device-and-how-to-Greenify
Reference: https://developer.android.com/training/scheduling/wakelock.html
It is needed when you don't want CPU to sleep when user locks the screen for example.
If you have an IntentService without acquired WakeLock it will pause after a while if user locks the screen and it will continue its work when user wakes a device. With WakeLock acquired your service will work even if the screen is locked.
As #My God mentioned, it impacts on battery life a lot, so, use it only when you really need to finish some operation and you cannot wait till user wakes a device.
I have service running in android to sync files when user wants.
This is not a 24/7 service, only runs for a period of file transfers over wifi, so the user when done transferring files quits the app and therefore the service exits.
So the scenario could be that user has left the mobile and it might get locked/screen off automatically.
Regardless of screen on or off is there a way to ensure wifi is always performing the same with low latency ?
There are multiple apis in relation to this, Wifi lock, Wifi-sleep-policy, screen lock .. People seem to use a combination of them to keep good wireless performance .
Is wifi lock enough to ensure it ? or do I need to use combination of APIs ?
Thank you.
EDIT: this post was helpful
PARTIAL_WAKE_LOCK vs SCREEN_DIM_WAKE_LOCK in download thread
To be on the safe side, a WifiLock as well as a WakeLock would be optimal, with the WakeLock being the more important lock to aquire. The WakeLock ensures the device stays on, and the WifiLock ensures the radio is operating. This should ensure consistent latency. However, you can't expect anything when it comes to data transfers so have your app be ready to handle random connection losses.
I am developing an android application and I want to reduce the power consumption. The method I believe is to put the phone into sleep mode whenever the user activity stops for a certain threshold period. I have three questions regarding this.
If I release the wakeLock and no other application is holding the wakeLock after how much time would the phone go to sleep?
I have multiple HandlerThreads running where I use sendMessageDelayed() function. Would these messages get delivered even after the phone goes to sleep mode?
Does putting the phone into aeroplane mode save more power rather than just putting the phone to sleep. if yes, then why is it because the only difference in those two modes is the use of cellular network.
If I release the wakeLock and no other application is holding the
wakeLock after how much time would the phone go to sleep?
There really is no definitive answer, but, from personal experience, I'd say it is likely that it will happen within 30 seconds to 1 minute.
I have multiple HandlerThreads running where I use
sendMessageDelayed() function. Would these messages get delivered even
after the phone goes to sleep mode?
I really wouldn't count on it because I've never seen anything that says it will wake up the device to send said Message. You can always test it, but I wouldn't trust it to work because the documentation does not claim that it will.
Does putting the phone into aeroplane mode save more power rather than
just putting the phone to sleep. if yes, then why is it because the
only difference in those two modes is the use of cellular network.
If you put it into sleep mode AND airplane mode, then you will save more battery than JUST sleep mode.
The reason for that is that even with the CPU pretty much asleep, the phone must keep a constant connection with the cellular network in order to know if you get a text or phone call. To do this, it must use the battery to constantly keep the antenna turned on. If you put it into airplane mode, it would basically turn the antenna off, and then the phone would not be using battery for that function.
The rules for holding cpu partial wake locks are very vague. Can someone explain when and why we should acquire one? When can the system decide the service should be suspended while doing some work on a thread?
I would like to understand the details of the cpu scheduler / activity / service manager.
Is the best place the android source code? if so which package?
Can someone explain when and why we should acquire one?
You acquire one when you need the CPU to stay running to complete some bit of work. You release it when that work is done.
So, for example, suppose you have an IntentService triggered by AlarmManager. You want the IntentService to check for new email messages for your email client. You would want to acquire a WakeLock so the CPU will not fall asleep while you are in the middle of doing that, then ensure that you release it. This sort of pattern is why I created the WakefulIntentService.
When can the system decide the service should be suspended while doing some work on a thread?
The fact that you are doing "some work on a thread" is immaterial. The CPU will fall asleep based on user inactivity. If the user is not keeping the device awake, and you are not keeping the device awake with a WakeLock, the device will fall asleep.
Having the CPU fall asleep is very important for power consumption. If you, say, decide that you are going to keep the CPU running 24x7, the battery will run dry relatively quickly.