Have you noticed a significant battery drain when debugging Android application on the phone?
Also, does a lot of Log.d() messages drains the battery?
Not really.
Is not your phone connected to your PC, and thus charging, while you perform debugging?
Every operation on Android drains battery, but I do not think Logging drain battery more than, say, drawing screen updates.
Yes! I just started debugging my Android apps on my phone (Motorola Droid) rather than on the emulator a few days ago because it's so much faster. Since then my battery life has become abysmal. Of course it doesn't get drained while I'm actually debugging because at that time the phone is connected to my PC and is charging from it. But the rest of the day my battery seems to drain much faster than it used to. When I look at the Battery Use screen in Settings it indicates that "Android OS" is using a significant amount of battery, far more than it ever did in the past. I have no idea what is causing this or how to fix it.
If your definition of "debugging Android application on the phone" is: you test your application while your phone is connected to your PC, then no. Your phone will charge all the time and the battery will get charged faster than it will drain, so you shouldn't have any worries.
On the other hand, if your phone isn't connected to PC, and you are calling Log.d() a LOT, let's say, 1,000,000 times in a loop, then yes, this will put a strain on your battery, since you will generate a lot of string statements that will appear under Logcat's logs and old logs will have to be discarded. Don't overdo it with logging messages.
Related
Recently I have developed an android app . The app is ok . But the user is complaining that app causing battery drain . Even some battery optimizer app (Like Battery Doctor) suggesting my app to force stop as it is causing battery draining too fast . There are a lot of service (like sync service , socket service) and broadcast receiver is used in this app and alarm manager is used in the app. How can I detect actually which is responsible for battery draining ?
I addressed the major contributors to battery drain in another of my posts.
If I understand correctly, you want to identify which section of code (including libraries) of your app is the culprit.
Here's my suggestion:
Confirm that there is a problem: Get as identical a phone configuration as possible, say by having the user send a dump of all processes running on their phone, and confirm there is a problem.
Confirm that it's your app: run tests with your app running and without. Make sure you take several measurements.
(A) If you have a profiler, use it to profile your application and identify potential issues. (B) If you don't have a profiler, create some dummy input, and start exercising your app while cutting out different functionality. Measure the battery drain.
NOTE: If the drain is small, detection becomes an issue. It's the old signal to noise ratio thing. The smaller the drain, the more measurements you have to take to determine what it is. Of course, the smaller the drain, the less of an issue it is.
I am developing a mobile monitoring system on Android, so I need a platform, that is mobile, lightweighted and that has some computing performance. I have chosen Android, but I've run to a problem that tablet's chargers cant catch up with power consumption.
I can't know for sure how much CPU will application consume (maybe 30%, 40%?), but let's say 100% to be sure.
Is there a way to achieve nonstop run? Maybe tablet with non-usb charger?
edit: Simply speaking I need to charge the device more quickly, then it consume power. Optimization and lowering power consumption by turning down hardware that is not needed right now doesnt seem as a solution since application can be used for entire time...
What you want is a a partial WakeLock.
WakeLock gives you the ability to stop the device from entering sleep-mode. There are several levels of Wakelock, you are (if I'm correct) interested in turning of the screen to minimize the power consumption but not put the device to sleep and halt computation. For this there is PARTIAL_WAKE_LOCK.
Make sure to have the right permission for your app before acquiring the WakeLock or you will get a security permission.
Another thing you might want to consider is to use a high-output charger that supplies more than the lowest rated chargers (500mA).
I've written a simple application that displays some text and a little bit of graphics on screen. I'm using a Nexus 7, this is for an installation, and as such needs to be on continuously as much as 12 hours a day. However, despite being plugged in and charging, my application drains the battery completely in about 8 hours (10-15% / hour).
Apart from the infrequently-changing text and graphics, my application does two things:
It keeps the screen on, at full brightness (via WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); and
It opens two DatagramSockets, one to act as a UDP client, and one to act as a UDP server. These sockets are both opened automatically and managed by the library I'm using, OSCP5. It's open source, so I can hack it as needed, though the less the better. (KISS.)
I've tested the Nexus 7 battery life with no apps running, screen at full brightness, with a tickle every 29 minutes to keep it from sleeping, for a few hours with no battery drain at all. So, signs seem to point to the sockets. I don't have frequent communication on those sockets, they receive approximately a few KB every minute (based on user interaction with another part of the installation).
Is there anything I can do, specifically with the sockets, or any other general testing, to minimize or eliminate the battery drain? I only need to get through 12 hours, but keeping full charge while plugged in doesn't seem like it should be such a tall order...
However, despite being plugged in and charging, my application drains the battery completely in about 8 hours (10-15% / hour).
What do you have the Nexus 7 plugged into? I think it can take advantage of a high-speed charging USB port, the kind you need for most tablets (Android + iPad). I could see your results being more likely if you have this plugged into a garden-variety USB port.
So, signs seem to point to the sockets.
They're certainly a candidate, though since the WiFi radio would probably say on while the device is charging anyway, I'd be a bit surprised. To me, this feels like a CPU issue, where you're busy-waiting or something rather than blocking.
Is there anything I can do, specifically with the sockets, or any other general testing, to minimize or eliminate the battery drain?
I'd see if Traceview points out where you're consuming lots of CPU time. I'd investigate the charging port issue. I'd modify your app to mock the socket work and try to see if your problem persists -- if it does, then the issue isn't the sockets.
Well, keeping the screen on full brightness will really suck down the power. The GPS will do it too; I think those are the two biggest power sinks.
Keeping the network radios busy and keeping the cpu busy will also suck down the power. I hope you're not busy-waiting in any of your software.
Other than that, all I can suggest is that you use the high-power charger that likely came with the tablet. USB data ports are limited to 500mA current, and I've worked with more than one device that can require more than that to keep the battery alive.
Three months and endless head-desk-banging later, it turns out the culprit was my USB run. The total length is around 20'-25', and the length is attenuating the power that gets all the way through the line. Solution: powered USB hub.
So, the answer ended up having nothing to do with Android or programming at all. Sorry, SO.
I am trying to find where in the android source code the event that tells the device to shut down due to low battery it located.
I am currently looking at cyanogenmod source but I imagine this is a common area for all mod sources.
Can anyone point out where this is and if possible how to prevent the phone from shutting down?
To clarify why I want to do this:
I have a faulty phone that always reports the battery level to be zero when I know it is not (I have tested the battery in other phones and other batteries in the faulty phone). Preventing it from shutting down would just extend the life of this otherwise useless phone.
in BatteryService.java ,when the battery lvl is too low (0%, lower than 3.4v),the service will call shutdownIfNoPower to shutdown system.
By the way, is your phone has been ADC calibrated ? or the calibration data had been destoryed....
Is there an Android API for stop charging even though physically the charging is on?
Reading previous answers, it looks like there's no way to do this exclusively in software. However, it should be relatively easy to do it with a little hardware help - i.e. by using any Bluetooth- or WiFi-enabled smart electrical switch with open API. The phone could then switch the charger on or off programmatically whenever it feels like it.
Is there an Android API for stop charging even though physically the charging is on?
AFAIK no.
Justification:
android.intent.action.BATTERY_CHANGED is a protected intent that can only be sent by the system. Therefore you cannot duplicate or override the functionality.
You guys are all missing the point - the purpose of limiting charging is to increase battery lifespan by keeping it away from fully charged conditions (and near empty but that's a different issue). If you anticipate needing a full charge then tell it to charge to 100.
If you can't set a charge limit then the battery would rarely be idle and ideally charged when on the charger. It probably requires device level support though.
https://play.google.com/store/apps/details?id=es.guille.stopcharge uses echo "0" > /sys/class/power_supply/battery/device/charge. It works for me on a nexus 4 (rooted), as long as the device isn't fully charged (in a charging state).
Also relevant: https://android.stackexchange.com/questions/15001/how-can-i-avoid-the-battery-charging-when-connected-via-usb
Contrary to some answers indicating it's not possible, it is, indeed possible with ROOT. You can find some apps on the Play Store that will allow you to do this (with root permission, of course).
The best app that works on every phone I've tried it on is Battery Charge Limit. You can download it from the Play Store or the XDA discussion thread. The app is under active development so, if you find that your phone isn't supported, just submit an issue on its GitHub page.
NB: I am not the developer of this application.
This is unfortunately not possible. Android only have read APIs for battery data.
Battery charging control is internal to the Android kernel and battery IC modules. Manufacturers do change it from time to time, like HTC is having small cycles from 95-100% (a bad idea IMO), Samsung does stop charges as soon as it reaches 100%, but I've never seen a device able to stop charging before 100% to reduce battery aging :(.
At best if we're lucky, a sysfs interface might help do that, but it requires root and will differ from device to device.
Just to add to this. Samsung phones do not stop charging after 100%. As far as I'm aware they "trickle" charge past this point however they recommend you do not charge your phone past 100% for over 1 hour. Battery manufacturers like Anker also state the same so the idea of stopping the charge at 100% would be really useful. The risks of leaving the phone on charge are battery bloat and eventually cell rupture. I wasn't aware of this to start off with an killed my samsung battery within 3 months of owning the phone.
To expand on the original question. I've noticed some kernels allow fast charging. If they are able to alter the charge rate up could we alter the charge rate down (trickle) to meet a desired finish time (when your morning alarm goes off)??
I have installed a timer on the AC receptacle where I plug my Moto E's charger into. After guestimating how much time the battery needs to charge to about 85%, I set the timer. This is not my idea of a solution, but at least it keeps my phone from being charged to 100%, as I usually err on the short side when setting the timer.
I dont think there would be such an api. If it does exist it needs to be supported by the device manufacture as its not a pure OS lever feature. The same reason would make it a bad idea to use it as not all devices will support it.
That being said it would be a bad idea to use/implement it , mobile devices are not built to directly run from the power supply and a user will not plug in the cable if he/she did not want to actually charge the device.