Example usage for android.os BatteryManager EXTRA_LEVEL

List of usage examples for android.os BatteryManager EXTRA_LEVEL

Introduction

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

Prototype

String EXTRA_LEVEL

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

Click Source Link

Document

Extra for android.content.Intent#ACTION_BATTERY_CHANGED : integer field containing the current battery level, from 0 to #EXTRA_SCALE .

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   www .j  a v a2s . c  o m*/

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

From source file:Main.java

public static int getBatteryLevel(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    return batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
}

From source file:Main.java

public static String getBatteryLevel(Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent status = context.registerReceiver(null, intentFilter);
    int level = status.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    return String.valueOf(level);
}

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  v  a2s  . co 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;
    }/*from   w  w w  .j  a  v a2s .co m*/
    return ((float) level / (float) scale) * 100.0f;
}

From source file:Main.java

public static int getBatteryLevel(Context context) {

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_BATTERY_CHANGED);
    Intent intent = context.registerReceiver(null, filter);
    if (intent == null) {
        return -1;
    }//from  w  ww.  j a  va 2s  .  c  o m
    return intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
}

From source file:Main.java

public static int getBatteryPercent(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    assert batteryStatus != null;
    return batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
}

From source file:Main.java

/**
 * return between 0 and 1./*from  w  ww  .  j a  v a  2s .c  om*/
 */
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;
}