Keeping services awake without draining battery - android

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.

Related

Keep Android awake for incoming network connections?

I'm writing an HTTP server for Android devices, implemented via NanoHTTPD.
A goal of mine is to have the device allow incoming connections even with the screen off.
I started small, with a persistent notification, thinking that would keep my app in memory and running in the background. After locking the device, I could keep navigating the webpages it serves up as long as I don't leave it alone for about a minute. Once I do, it totally stops responding.
I escalated my attempt by including a CPU partial wakelock, which made no difference. I then added a full WifiLock to keep the radio on, and finally, in desperation, a MulticastLock (I thought maybe it'd keep the radio listening for connections). Still, after not making any connections for about a minute, the device stops responding, even with all these locks.
Is there anything specific I can do to keep the device listening for incoming connections? It seems like hitting the device with periodic requests keeps it awake... can I somehow emulate this behavior programmatically? I cannot think of a way.
Thanks!
EDIT: for the purpose of this question, battery drain can be disregarded.
EDIT: NanoHTTPD is being run as a service, as well.
You need the following:
Foreground service
Wifi lock
CPU lock
Incomplete snippets (eg missing the services / relinquishing the locks):
// Keep the CPU awake.
powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Httpd");
wakeLock.acquire();
// Keep the wifi awake.
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "Httpd");
wifiLock.acquire();

Is it possible to start the phone without pressing power button?

I have an app which is running on an android device which has no battery. It's just charging all the time and the app is running. But sometimes the device is not charging and logical to that the phone will turn off.
When it's charging again the device should start from itself and boot. The user shouldn't press the power button. That's the requirement.
Is this possible anyway?
Can I achieve this when I root the device?
Has anybody experiences with that case?
in short: No.
ADB is not running when the phone is powered off, and as #langerhans said, wake on alarm is proprietary
You could do a hardware solution where you hijack the battery pins to a power station or modified charger. (make sure you do not feed any power through usb at the same time)
It should be cheap and quite easy to build.
Generally, this is not possible. It is highly device dependant. Some devices allow alarms turning on the phone to ring as an alarm clock even if they are turned off. But this is a proprietary feature and therefore not easily accesible to custom applications.
I have basically the same problem running an Odroid device which luckily turns itself on after power loss, but if I shut it down manually, I can only turn back on with pulling the power.
The only thing I could think off would be an extra watchdog device, but I have no idea if something like that exists. Maybe you'd need to build it yourself.

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

How to keep a TCP connection established indefinitely?

I need to keep a TCP connection established indefinitely (as far as possible). Is not an own server so we cannot change the way it works. This server needs some kind on ping each minute to know that the connection is alive. If the server do not receive the ping after some minutes (less than five), the connection and the session is closed. In this way i need to maintain a TCP connection with the server and be able to send pings at the periods specified.
At the moment i have an Android service with the flag "ongoing" so Android should not kill it (at least by normal procedures). The android service seems to run fine and the pings are sent periodically. However when the service is running on a mobile phone (not emulator) and it gets idle, android seems to freeze the service while the CPU is sleeping, so the TimerTask sending pings stop working and the connection goes down.
I have tried to lock the phone from going to sleep with a partial wake lock and it solves the problem but the phone consumes too battery, what is unfeasible.
I noticed that the AlarmManager may help on this task, so i want to schedule an alarm to update the running service and then send the ping. This will let the CPU going to sleep and also the ping being sent. But schedule an alarm each minute may be also so battery consuming or not?
I have not tested this approach at the moment, but is feasible? is there a better way to keep this kind of TCP connection?. How android services like Gmail solves this kind of issues?
I need to keep a TCP connection established indefinitely with a server.
Why?
For starters, it is technically impossible. Users will switch between networks (e.g., was on WiFi, failed over to 3G), users will leave areas where they have Internet coverage, users will turn on airplane mode, etc.
At the moment i have an Android service with the flag "on_course" so Android should not kill it.
There is no such concept as "on_course" in Android. Android services can and will be killed by users (task killer, force-stop in Manage Services) or by the OS. And, since this will be an everlasting services, your users will kill your service if they do not understand what value it is continuously delivering.
I have tried to lock the phone from going to sleep with a partial wake lock and it solves the problem but the phone consumes too battery, what is unfeasible.
Correct.
I noticed that the AlarmManager may help on this task, so i want to schedule an alarm to update the running service and then send the ping. This will let the CPU going to sleep and also the ping being sent. I have not tested this approach at the moment, but is feasible?
Sure. You will still consume too much battery, IMHO. Please allow your user to control your polling period, with a wide range of options (e.g., 10 minutes, 30 minutes, one hour, never).
How android services like Gmail solves this kind of issues?
They use C2DM, which is part of the OS and took a lot of engineering to get right. There was a presentation on it at the 2010 Google I|O conference -- the video should be on YouTube. Note that they do the heartbeat ping every 30 minutes IIRC, and they optimize for the case where the device is on 3G (to allow the WiFi radio to turn off after inactivity).
If your objective of your permanent connection is to implement a push-style communications channel, please consider using C2DM.
You can wake phone from idle with partial wake lock right before you execute ping and after you do it release wakelock. It will scientifically reduce battery consumption and it will do the job. Also, try to increase ping time as long as you can.
5 Minutes is a little short for the ping time, can you configure the server to hold the connection longer? 30 minute ping or higher would be ideal. Here is an example of an application that holds a background tcp connection and will wake up the device from a deep sleep on incoming tcp traffic. https://github.com/schwiz/android-websocket-example

Android Froyo and Wifi

By default, Wifi sleep policy is "Sleep on screen idle".
With this policy, is it possible for a Background Service at a later time to wake up Wifi using some API?
Am trying the following, but does not work:
When my Background Service wakes up, it calls "ConnectivityManager.getActiveNetworkInfo()" to get active network.
Since, the wireless is off on idle, I tried waking it up using "WifiManager.startScan" on a previously used Wifi connection.
But still dont get Wifi connectivity.
Any ideas?
I preferably do not want to change my sleep policy to "Never".
Thanks
Hemant
There are no real simple solutions for this. To with a high probability ensure you have WIFI connectivity when the phone/screen goes to sleep the best way is to turn it off. Look here for a lot of details - http://wififixer.wordpress.com/
It is important to realize that in sleep mode the Wifi enters a low power mode. This will become tricky then to programmatically check as it might have connectivity to the Wifi but the Wifi connection is too weak or too slow to complete the HTTP request and hence it times out. This would force you to also check the speed of the Wifi connectivity as well as you will have an active network but a pretty lousy one.
Proper handling of the escaping when timeout occurs for the HTTP call you make makes it ok to use but ultimately the only way to have a background thread constantly running to get data is only doable when you have the Wifi mode to never sleep.
It is tricky and not the best way I know. :-( It is however the only path I have found which is reliable enough.

Categories

Resources