Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 2.0 Best Method to Check Battery Level/Charging?

Okay let me start off by saying I have been looking around a lot for an answer to this question. I am trying to figure out what is best practice and what best for battery life. Here is my situation:

I would like my application to suspend its activities when the device reaches a certain user defined battery level and not have to use the Intent.ACTION_BATTERY_LOW and Intent.ACTION_BATTERY_OKAY. I have seen this option in a lot of programs and would like to emulate it with my application.

I understand that the Intent.ACTION_BATTERY_CHANGED must be a registered event. You can not just declare it in the manifest for a BroadcastReceiver to get that receiver to get the Intent. I know how to code up how to get the battery level / scale and see if the device is being charged via USB or AC

My current thought process has brought me to 2 choices and I can't find out which is the best option or if there is another option that I am not aware of?? Maybe someone could help me with the pros and cons of the options to help me out??

Option 1: Create a repeating alarm using the AlarmManager to basically poll the device every so often to check the battery level. So that when my alarm fires it would send a custom Intent for my manifest registered receiver to check the battery level.
PRO no background service running that could get killed or eat up battery life.
CONs not having realtime detection of plugging/unplugging the device from a charging source and having to rely on the next fired alarm to detect the change.

Option 2: Create a service that registers a receiver for Intent.ACTION_BATTERY_CHANGED so it will not only get the battery level when it is broadcast by the system, but will detect the changes in charging realtime.
PRO realtime detection of battery life change and changes to charging method.
CON a constant service running that could get killed
CON would eat up processing time and battery life in order to keep the detection going



Battery Level:

Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED

Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
                    new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int rawlevel = batteryIntent.getIntExtra("level", -1);
double scale = batteryIntent.getIntExtra("scale", -1);
double level = -1;
if (rawlevel >= 0 && scale > 0) {
    level = rawlevel / scale;



Plugged In/Charging:

android USB connection charging signals

public static boolean isConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

}


Thanks in advance
-H

like image 698
Havoc Avatar asked Jan 24 '12 15:01

Havoc


People also ask

How do I check battery charge on Android?

Show battery percentage in status barOpen your phone's Settings app. Tap Battery. Turn on Battery percentage.

How check battery consumption in android programmatically?

Go to the activity_main. xml file, which represents the UI of the project. Add a Button, so whenever the user will click on the Button a Toast message with battery percentage will be popped up on the screen.


1 Answers

Here is the solution I came up with

I created an Alarm to fire off X number of seconds, I think I set it to 90, to send a custom Intent. I then found that android.intent.action.ACTION_POWER_CONNECTED and android.intent.action.ACTION_POWER_DISCONNECTED allowed me to check to see if the device was plugged in or not. Using a combination of the above mention functions I was able to check my battery level and if the device was charging without having to create a constant service with a registered receiver. I hope this helps at least someone out there.

like image 62
Havoc Avatar answered Nov 15 '22 04:11

Havoc