Socket connections and Polling. Which is a better solution in terms of battery life?

Keeping an idle TCP socket connection open (with no data being sent or received) will not (or at least, should not) consume any more battery than having it closed. That is because an idle TCP connection uses no bandwidth or CPU cycles(*). That said, keeping a TCP connection open for extended periods may not be … Read more

Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED

This is how to get the battery level without registering a receiver: Intent batteryIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); double level = -1; if (rawlevel >= 0 && scale > 0) { level = rawlevel / scale; } It can use a null BroadcastReceiver because of … Read more

Get battery level and state in Android

Tutorial For Android has a code sample that explains how to get battery information. To sum it up, a broadcast receiver for the ACTION_BATTERY_CHANGED intent is set up dynamically, because it can not be received through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). public class Main extends Activity { private … Read more