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:saphion.services.ForegroundService.java

public Status readBatteryStat() {
    Intent batteryIntent = getBaseContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    isconnected = (plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB
            || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS);
    level = -1;/*  ww  w. j av a 2  s . c o  m*/
    if (rawlevel >= 0 && scale > 0) {
        level = (int) ((rawlevel * 100) / scale);
        Log.d("rawLevel: " + rawlevel);
        Log.d("scale: " + scale);
    }

    if (mPref.getBoolean(PreferenceHelper.KEY_ONE_PERCENT_HACK, false)) {
        try {
            java.io.FileReader fReader = new java.io.FileReader(
                    "/sys/class/power_supply/battery/charge_counter");
            java.io.BufferedReader bReader = new java.io.BufferedReader(fReader);
            int charge_counter = Integer.valueOf(bReader.readLine());
            bReader.close();

            if (charge_counter > PreferenceHelper.CHARGE_COUNTER_LEGIT_MAX) {
                disableOnePercentHack("charge_counter is too big to be actual charge");
            } else {
                if (charge_counter > 100)
                    charge_counter = 100;

                level = charge_counter;
            }
        } catch (java.io.FileNotFoundException e) {
            /*
             * These error messages are only really useful to me and might
             * as well be left hardwired here in English.
             */
            disableOnePercentHack("charge_counter file doesn't exist");
        } catch (java.io.IOException e) {
            disableOnePercentHack("Error reading charge_counter file");
        }
    }

    return new Status(level, isconnected);
}

From source file:com.lemon.lime.MainActivity.java

private void getBatteryPercentage() {
    BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

            if (currentLevel >= 0 && scale > 0) {
                level = (currentLevel * 100) / scale;
            }//from   ww  w .j a v a 2  s . c o m
        }
    };
    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

From source file:com.landenlabs.all_devtool.ConsoleFragment.java

private int calculateBatteryLevel(Context context) {
    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
    return level * 100 / scale;
}

From source file:in.codehex.arrow.MainActivity.java

/**
 * Get the battery status of the device/*from   w  w  w .  j a  va2  s.c  om*/
 *
 * @return the battery level of the device
 */
private float getBatteryLevel() {
    Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = -1, scale = -1;
    if (batteryIntent != null) {
        level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        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:com.mobilyzer.util.PhoneUtils.java

private synchronized void updateBatteryStat(Intent powerIntent) {
    int scale = powerIntent.getIntExtra(BatteryManager.EXTRA_SCALE, Config.DEFAULT_BATTERY_SCALE);
    int level = powerIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, Config.DEFAULT_BATTERY_LEVEL);
    // change to the unit of percentage
    this.curBatteryLevel = level * 100 / scale;
    this.isCharging = powerIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
            BatteryManager.BATTERY_STATUS_UNKNOWN) == BatteryManager.BATTERY_STATUS_CHARGING;

    Logger.i("Current power level is " + curBatteryLevel + " and isCharging = " + isCharging);
}

From source file:org.akvo.caddisfly.sensor.colorimetry.liquid.ColorimetryLiquidActivity.java

private void saveImageForDiagnostics(@NonNull byte[] data, String result) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = registerReceiver(null, intentFilter);
    int batteryPercent = -1;

    if (batteryStatus != null) {
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        batteryPercent = (int) ((level / (float) scale) * 100);
    }/*from   www. ja v a 2 s  . co  m*/

    if (mIsCalibration) {
        result = String.format(Locale.US, "%.2f", mSwatchValue);
    }

    String date = new SimpleDateFormat("yyyy-MM-dd_HH-mm", Locale.US).format(new Date());
    ImageUtil.saveImage(data, CaddisflyApp.getApp().getCurrentTestInfo().getId(),
            String.format(Locale.US, "%s_%s_%s_%d_%s", date, (mIsCalibration ? "C" : "T"), result,
                    batteryPercent, ApiUtil.getInstallationId(this)));
}

From source file:org.metawatch.manager.Monitors.java

private static void createBatteryLevelReciever(Context context) {
    if (batteryLevelReceiver != null)
        return;// w w  w .  j  a va  2s  .co m

    batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = -1;
            if (rawlevel >= 0 && scale > 0) {
                level = (rawlevel * 100) / scale;
            }
            boolean charging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) > 0;
            if (BatteryData.level != level || BatteryData.charging != charging) {
                //if (Preferences.logging) Log.d(MetaWatch.TAG, "Battery level changed: "+rawlevel+"/"+scale+" - "+level+"%");
                BatteryData.level = level;
                BatteryData.charging = charging;
                Idle.updateIdle(context, true);
            }
        }
    };
    context.registerReceiver(batteryLevelReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

From source file:com.dsna.android.main.MainActivity.java

public void logBatteryLevelAndProcessTime(long processedTime) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    batteryStatus = getApplicationContext().registerReceiver(null, ifilter);
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    float batteryPct = level / (float) scale;
    batteryPct = Math.max(Math.min(batteryPct, 1), 0);
    logBatteryAndProcessResult.append(System.currentTimeMillis());
    logBatteryAndProcessResult.append(',');
    logBatteryAndProcessResult.append(batteryPct);
    logBatteryAndProcessResult.append(',');
    logBatteryAndProcessResult.append(processedTime);
    logBatteryAndProcessResult.append('\n');
}

From source file:org.pixmob.freemobile.netstat.MonitorService.java

private int getBatteryLevel() {
    if (batteryIntentFilter == null) {
        return 100;
    }/*from   w  w w . j ava2  s . c o m*/
    final Intent i = registerReceiver(null, batteryIntentFilter);
    if (i == null) {
        return 100;
    }

    final int level = i.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    final int scale = i.getIntExtra(BatteryManager.EXTRA_SCALE, 0);

    return scale == 0 ? 100 : (int) Math.round(level * 100d / scale);
}

From source file:com.droid.app.fotobot.FotoBot.java

/**
 *  ? ?. ?  ,     battery_level.//from   w  w  w.  j ava 2s .  co m
 */
public void batteryLevel() {

    BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            //context.unregisterReceiver(this);

            int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            battery_temperature = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1)) / 10.0f;
            battery_level = -1;
            if (rawlevel >= 0 && scale > 0) {
                battery_level = (rawlevel * 100) / scale;
            }

            //   int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            //  isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
            //   if (VERSION.SDK_INT > VERSION_CODES.JELLY_BEAN) {
            //       isPlugged = isPlugged || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
            //   }

        }
    };

    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);

}