Example usage for android.content Intent ACTION_BATTERY_CHANGED

List of usage examples for android.content Intent ACTION_BATTERY_CHANGED

Introduction

In this page you can find the example usage for android.content Intent ACTION_BATTERY_CHANGED.

Prototype

String ACTION_BATTERY_CHANGED

To view the source code for android.content Intent ACTION_BATTERY_CHANGED.

Click Source Link

Document

Broadcast Action: This is a sticky broadcast containing the charging state, level, and other information about the battery.

Usage

From source file:Main.java

public static boolean isChargingOrFull(Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, intentFilter);
    return isChargingOrFull(batteryStatus);
}

From source file:Main.java

public static float level(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed but I added just in case.
    if (level == -1 || scale == -1)
        return 50.0f;

    return ((float) level / (float) scale) * 100.0f;
}

From source file:Main.java

private static Float getBatteryLevel(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed but I added just in case.
    if (level == -1 || scale == -1) {
        return 50.0f;
    }/*w  w w . ja v  a2 s  . co  m*/

    return ((float) level / (float) scale) * 100.0f;
}

From source file:Main.java

/**
 * return between 0 and 1.//from ww w.  j a  v  a  2s  .  c  o  m
 */
public static float level(final Context context) {
    final IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    final Intent batteryStatus = context.registerReceiver(null, ifilter);
    final int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    final int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    return level / (float) scale;
}

From source file:Main.java

public static float getBatteryLevel(Context applicationContext) {
    Intent batteryIntent = applicationContext.registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed but I added just in case.
    if (level == -1 || scale == -1) {
        return 50.0f;
    }/*from w w w .j ava  2  s  .  c om*/
    return ((float) level / (float) scale) * 100.0f;
}

From source file:Main.java

/**
 * Gets the battery info of device, and then writes it to a file.
 * /*from  w  w w .  j av  a  2s  .  c o  m*/
 * @param context the context of application
 */
public static String getBatteryUsageInfo(Context context) {
    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int rawlevel = batteryIntent.getIntExtra("level", -1);
    double scale = batteryIntent.getIntExtra("scale", -1);
    return String.format("Device has %s of %s battery left", rawlevel, scale);
}

From source file:Main.java

/**
 * @param context/* ww w .j a va  2s. com*/
 * @return BatteryManager.BATTERY_PLUGGED_USB, BatteryManager.BATTERY_PLUGGED_AC, BatteryManager.BATTERY_PLUGGED_WIRELESS
 */
public static int getChargingMode(Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, intentFilter);
    return getChargingMode(batteryStatus);
}

From source file:Main.java

public static int getCurrentBatteryChargePercentage(Context context) {
    IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, iFilter);

    final int DefaultValue = 0;

    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, DefaultValue);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, DefaultValue);

    int batteryPct = calculateBatteryPercentage(level, scale);

    return batteryPct;
}

From source file:Main.java

public static boolean isCharging(Context context) {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, filter);
    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
    boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
    boolean wirelessCharge = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS);
    }/* w  w  w  .  j  a  v  a 2  s  .  c o m*/
    return (usbCharge || acCharge || wirelessCharge);
}

From source file:Main.java

public static boolean isCharging(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);

    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}