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

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 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;
    }/*  ww w. ja v  a2  s .c  om*/

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

From source file:Main.java

/**
 * @see #isPlugged(android.content.Context)
 * @see #isPlugged(android.content.Intent)
 *//*from w w  w .  j  a v a2 s.  c  o m*/
@SuppressLint("InlinedApi")
public static int getBatteryLevel(@Nullable Intent intent) {
    if (intent == null) {
        return 100;
    }

    final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    final int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    return level * 100 / scale;
}

From source file:com.mendhak.gpslogger.common.Systems.java

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

    if (level == -1 || scale == -1) {
        return 50;
    }//from  w w  w  .  jav  a2 s .com

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

From source file:org.mozilla.mozstumbler.service.scanners.ScanManager.java

public boolean isBatteryLow() {
    Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent == null)
        return false;

    int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING);
    int level = Math.round(rawLevel * scale / 100.0f);

    final int kMinBatteryPct = 15;
    return !isCharging && level < kMinBatteryPct;
}

From source file:org.digitalcampus.oppia.utils.MetaDataUtils.java

private float getBatteryLevel() {
    Intent batteryIntent = ctx.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  av  a  2  s.  co m*/

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

From source file:rus.cpuinfo.AndroidDepedentModel.BatteryInfo.java

@NonNull
private String getBatteryLevel() {
    final Integer level = getIntExtra(BatteryManager.EXTRA_LEVEL);
    final Integer scale = getIntExtra(BatteryManager.EXTRA_SCALE);

    return (level | scale) == -1 ? StringUtils.EMPTY : String.valueOf(((float) level / (float) scale) * 100.0f);
}

From source file:com.hmatalonga.greenhub.managers.sampling.DataEstimator.java

@Override
public void onReceive(Context context, Intent intent) {
    if (context == null) {
        LOGE(TAG, "Error, context is null");
        return;//from w  w  w  .j a  va 2s . c  o m
    }

    if (intent == null) {
        LOGE(TAG, "Data Estimator error, received intent is null");
        return;
    }

    LOGI(TAG, "onReceive action => " + intent.getAction());

    if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
        try {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            mHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
            plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            present = intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
            status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            technology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
            temperature = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)) / 10;
            voltage = ((float) intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0)) / 1000;
        } catch (RuntimeException e) {
            e.printStackTrace();
        }

        // We don't send battery level alerts here because we need to check if the level changed
        // So we verify that inside the DataEstimator Service

        if (temperature > SettingsUtils.fetchTemperatureWarning(context)) {
            if (SettingsUtils.isBatteryAlertsOn(context) && SettingsUtils.isTemperatureAlertsOn(context)) {

                // Check temperature limit rate
                Calendar lastAlert = Calendar.getInstance();
                long lastSavedTime = SettingsUtils.fetchLastTemperatureAlertDate(context);

                // Set last alert time with saved preferences
                if (lastSavedTime != 0) {
                    lastAlert.setTimeInMillis(lastSavedTime);
                }
                int minutes = SettingsUtils.fetchTemperatureAlertsRate(context);

                lastAlert.add(Calendar.MINUTE, minutes);

                // If last saved time isn't default and now is after limit rate then notify
                if (lastSavedTime == 0 || Calendar.getInstance().after(lastAlert)) {
                    // Notify for temperature alerts...
                    if (temperature > SettingsUtils.fetchTemperatureHigh(context)) {
                        Notifier.batteryHighTemperature(context);
                        SettingsUtils.saveLastTemperatureAlertDate(context, System.currentTimeMillis());
                    } else if (temperature <= SettingsUtils.fetchTemperatureHigh(context)
                            && temperature > SettingsUtils.fetchTemperatureWarning(context)) {
                        Notifier.batteryWarningTemperature(context);
                        SettingsUtils.saveLastTemperatureAlertDate(context, System.currentTimeMillis());
                    }
                }
            }
        }
    }

    // On some phones, scale is always 0.
    if (scale == 0)
        scale = 100;

    if (level > 0) {
        Inspector.setCurrentBatteryLevel(level, scale);

        // Location updates disabled for now
        // requestLocationUpdates();

        // Update last known location...
        // if (lastKnownLocation == null) {
        //    lastKnownLocation = LocationInfo.getLastKnownLocation(context);
        // }

        Intent service = new Intent(context, DataEstimatorService.class);
        service.putExtra("OriginalAction", intent.getAction());
        service.fillIn(intent, 0);

        if (SettingsUtils.isPowerIndicatorShown(context)) {
            LOGI(TAG, "Updating notification status bar");
            Notifier.updateStatusBar(context);
        }

        EventBus.getDefault().post(new BatteryLevelEvent(level));

        startWakefulService(context, service);
    }
}