Example usage for android.os BatteryManager EXTRA_SCALE

List of usage examples for android.os BatteryManager EXTRA_SCALE

Introduction

In this page you can find the example usage for android.os BatteryManager EXTRA_SCALE.

Prototype

String EXTRA_SCALE

To view the source code for android.os BatteryManager EXTRA_SCALE.

Click Source Link

Document

Extra for android.content.Intent#ACTION_BATTERY_CHANGED : integer containing the maximum battery level.

Usage

From source file:Main.java

public static float getBatteryLevel(Intent batteryIntent) {
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    return level / (float) scale;
}

From source file:Main.java

public 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;
    }/*from   w  w w.  java  2  s .c o  m*/

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

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;
    }//from  w ww.  ja va 2 s  .  c  o  m

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

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;
    }// w w  w.  j  a v  a  2 s.c o  m
    return ((float) level / (float) scale) * 100.0f;
}

From source file:Main.java

/**
 * return between 0 and 1./*from ww  w.  j  ava 2  s. co 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 getChargePct(Intent batteryStatus) {
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    return level / (float) scale;
}

From source file:Main.java

public static float getBatteryLevel(Context context) {
    Intent batteryStatus = getBatteryStatus(context);
    if (batteryStatus != null) {
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        return (level / (float) scale);
    } else {/*w  w  w  . j  a  v a  2s  .  c o  m*/
        return 0.5f;
    }
}

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 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, just in case.
    if (level == -1 || scale == -1) {
        return 0.50f;
    }//from w  ww. j  a  va 2  s . c  o m

    return ((float) level / (float) scale);
}