how to minimize battery drain in a heavy service - android

I would like create a service to do something when some "hot situation" occurs, and by "hot situation" I mean the following:
the GPS/cell coordinates are in a known zone
a known Bluetooth device is detected
a known Wi-Fi network is detected
weather info has changed
considerable change in acceleration of device detected: eg. from walking to travel by car.
As one can see, these tasks are "heavy" and will be run frequently – say, 15 minutes, but sometimes even more – when I have to detect the acceleration of the device.
The question I have is this: will this service drain the battery heavily?
If so, what recommendations do you have to minimize battery drain from the tasks described above?

This might be of interest to you:
Coding for battery life

GPS is the top battery drainer - by having it on all the time, the battery duration will probably be cut down to half. Wireless connection is also quite a big drainer. If your app is working via the cell network, then if the phone is connecting to 3G then that probably drains as quickly as wireless connection anyway (maybe even more)...
I would suggest rethinking the usage of your service. Maybe make it into an app and have the user explicitly turning it on when he thinks to be in a "hot situation". Leave it as a service and the user might start thinking of your app as some sort of malfunctioning program and that will hurt severely your downloads/sales.
Also, to keep track of your apps battery usage, you can visit the "Battery usage panel" that lives inside Android preferences.

Related

Android : Battery drains around 4 hours common with wifi ON and screen ON always

My app is single activity app which is used to generate token slip using WiFi printer over wifi which are connected locally. So the WiFi is always ON. Also the screen is always ON.
I have set android:keepScreenOn="true" in my activity xml file for this.
The tablet is exclusively used for this single app only not more than that. Even though, the battery drains around 4 hours.
Is this common? Or Would I change anything to achieve good battery backup?
Generally if your device is old, There is a chance to expect this kind of situation.
To debug about your network traffic, Follow this link
The network traffic generated by an app can have a significant impact
on the battery life of the device where it is running. In order to
optimize that traffic, you need to both measure it and identify its
source. Network requests can come directly from a user action,
requests from your own app code, or from a server communicating with
your app.
Link is here

How to check battery consumption caused by Bluetooth Scans

I have an Android application to detect Eddystone beacons using the Android Beacon Library. I would like to test the battery consumption caused by the application to find the optimal background and foreground scan rates. In Settings > Apps > MyBeaconDetectorApp, it shows 0% battery usage after having the bluetooth On for more than an hour. I think the application is in background and hence it is 0%. However, how do I check the battery consumption caused by Bluetooth scans. I have set my BackgroundBetweenScanPeriod as 1 minute.
Thanks
While it is difficult to measure the battery consumption directly, here is the technique I use:
Use a test device with a limited number of apps installed that will be running in the background. A stock Nexus device after factory reset is ideal.
Charge the battery to 100%
Put the device into airplane mode and put bluetooth on.
Install your app start it running in the background. Turn off the screen, and disconnect the device's USB connector.
Note the start time.
Let the app run in the background for at least 8 hours, ideally in the presence of at least one beacon.
Measure the battery level. The delta from 100% is the percentage drain. Note this number and the end time.
Look up how many mAH your battery has for your phone model. Multiply the percentage drain by the mAH and divide by the time of the test in hours. This tells you how many mA the phone uses when running your app.
Next, uninstall your app, and repeat the procedure above. This will give you a baseline battery drain for your device without your app running.
Finally, subtract the mAH baseline from the mAH with your app running to measure the additional mAH used by your app.
One final note: if you are really scanning every minute in the background (the default is every five minutes to save battery), I would expect this may cause a significant additional battery drain. Testing using the procedure above will tell you for sure. I'd love to hear your results!

Does android-wear watch keep the accelerometer always on to turn on the screen automatically? [duplicate]

I'm creating an Android Wear app that tries to detect some of the hand movements, to do it, I need to continuously monitor the accelerometer output. I'm wondering how it will affect the battery life.
For phones, I know that there are methods like "disable accelerometer while screen is turned off" to save battery, but what's the battery cost in case of watches? Since Android watch can count your steps and it turns on the screen when you face it towards your face, I believe the accelerometer is turned-on all the time anyway.
in this case, will my app drain the battery?
(Calculations after receiving the accelerometer values will be pretty simple.)
I think most android wear devices keeps the accelerometer on all the time to monitor steps, tilt-to-wake etc. However, I think most or all of this devices handles this in a low powered CPU (Sensor hub) to allow the main CPU to sleep most of the time when the screen isn't on.
If you want to monitor the accelerometer from an app, it would require the main CPU to be up and running at all times and cause a huge battery drain (20-40 mAh). I've tried this in an attempt to create a sleep tracker and drained about 70% battery over night.
You could consider batching which allows you to continuously monitor the accelerometer while allowing the main CPU to sleep most of the time. Basically you tell the sensor subsystem to gather data while the rest of the system sleeps and you just set an alarm to wakeup at a fixed interval (around 10 s for most android wear devices) to gather the latest batch of data.

Android opportunistic communication vs power management (what happens to service when phone goes to sleep)

I am writing an application for android that will use opportunistic communication. So when two devices are nearby they exchange data. For now it will happen when they are in the same wifi network (using mdns) or when they have paired bluetooth interfaces.
I was wondering from power manager point of view. Will my ongoing service be paused when phone enters sleep mode? If yes how do I prevent it?
Also any general tips how to make it consume as little power as possible? I was thinking to wake it up from time to time, but then there's a chance that it will miss a mdns query sent by other device which I want to avoid to maximize throughput.
And just to clarify - yes, I do want to use opportunistic communication and no I do not want to use any solution that involves connecting to the internet.
Any help appreciated.
Use Wakelock to indicate that your application needs to have the device stay on.
but it consumes considerable amount of power.
you can also use AlarmManager to start your app at that particular time when wifi is available

Keeping services awake without draining battery

I have an application that needs to continuously listen for incoming requests over wifi. A service that runs in the background does this job. However, this service falls asleep after a while when the screen turns off.
The solution from what I have searched is to use AlarmManager to keep it awake. But it is said that this will drain the battery of the device.
So, is there another way to do this?
For eg, what do apps like Whatsapp and Skype do? They don't seem to kill too much battery but they have continuously running services right?
Also, in case AlarmManager is the only way, it would be really kind if someone could share a tutorial or example for it.
The solution from what I have searched is to use AlarmManager to keep it awake
That will not help. Once the device falls asleep, your socket connection will be terminated. You would need to use a partial WakeLock plus a WifiLock to keep the device powered on continuously.
But it is said that this will drain the battery of the device.
The WakeLock and WifiLock will definitely drain the battery.
So, is there another way to do this?
Not if you need to use WiFi.
For eg, what do apps like Whatsapp and Skype do?
They do not use WiFi when the device wants to go to sleep. Once the WiFi radio powers down, they use mobile data, so no WifiLock is needed. For mobile data, incoming packets will wake up the device, so you only need a WakeLock while you are actually doing work, rather than constantly.
The best answer is to switch to use C2DM, though.
Actually its not your service which falls to sleep, its your WiFi unit on the device. Manufacturers like HTC (or perhaps all Android devices) have implemented this kind of behavior on their devices in which the WiFi unit goes standby after certain time period of screen-off. This helps the devices to save battery when its not being used.

Categories

Resources