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

/**
 * @see #isPlugged(android.content.Context)
 * @see #isPlugged(android.content.Intent)
 *///from  ww  w. j a  v  a 2  s.  co  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  ww  w  .j  a v  a  2  s  .  c o m*/

    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;
    }//  w w  w  . 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;// w w  w  .  j  a va2  s  . c  om
    }

    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);
    }
}

From source file: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  .  jav  a2  s . com*/
    }

    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);
            health = 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();
        }

        if (SettingsUtils.isBatteryAlertsOn(context)) {
            // Notify for temperature alerts...
            if (temperature > 45) {
                Notifier.batteryHighTemperature(context);
            } else if (temperature <= 45 && temperature > 35) {
                Notifier.batteryWarningTemperature(context);
            }
        }

        if (SettingsUtils.isPowerIndicatorShown(context)) {
            Notifier.updateStatusBar(context);
        }
    }

    // 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);
        service.putExtra("distance", distance);

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

        startWakefulService(context, service);
    }
}

From source file:mx.klozz.xperience.tweaker.fragments.BatteryInfo.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getActivity();/*from w w  w.jav a2s.  c  om*/
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    batteryinfoReveiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
            int leve = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            int Temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);

            level = leve * scale / 100;

            mBattery_Percent.setText(level + "%");//Mostraremos el porcentaje de bateria

            //Para los ajustes de voltaje siempre y cuando el kernel soporte dicha funcion.
            if (new File(BAT_VOLT_PATH).exists()) {
                int volt = Integer.parseInt(Helpers.LeerUnaLinea(BAT_VOLT_PATH));
                if (volt > 5000)
                    volt = (int) Math.round(volt / 1000.0); //Microvolts :)
                mBattery_volt.setText(volt + " mV");
            }

            switch ((int) Math.ceil(level / 20.0)) {
            case 0:
                mBatteryIcon.setImageResource(R.drawable.battery0);
                break;
            case 1:
                mBatteryIcon.setImageResource(R.drawable.battery1);
                break;
            case 2:
                mBatteryIcon.setImageResource(R.drawable.battery2);
                break;
            case 3:
                mBatteryIcon.setImageResource(R.drawable.battery3);
                break;
            case 4:
                mBatteryIcon.setImageResource(R.drawable.battery4);
                break;
            case 5:
                mBatteryIcon.setImageResource(R.drawable.battery5);
                break;
            }

            mBattery_Status.setText(
                    (Temp / 10) + " C " + getResources().getStringArray(R.array.batt_status)[status]);
        }
    };
    setRetainInstance(true);
    setHasOptionsMenu(true);
}

From source file:com.brewcrewfoo.performance.fragments.BatteryInfo.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getActivity();//from   ww w.j  av a 2 s. c o  m
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    batteryInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //int  health= intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
            //String  technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
            //boolean  present= intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
            //int  rawvoltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);

            plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
            int lev = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);

            level = lev * scale / 100;
            mbattery_percent.setText(level + "%");
            if (new File(BAT_VOLT_PATH).exists()) {
                int volt = Integer.parseInt(Helpers.readOneLine(BAT_VOLT_PATH));
                if (volt > 5000)
                    volt = (int) Math.round(volt / 1000.0);// in microvolts
                mbattery_volt.setText(volt + " mV");
            }
            switch ((int) Math.ceil(level / 20.0)) {
            case 0:
                mBattIcon.setImageResource(R.drawable.battery_0);
                break;
            case 1:
                mBattIcon.setImageResource(R.drawable.battery_1);
                break;
            case 2:
                mBattIcon.setImageResource(R.drawable.battery_2);
                break;
            case 3:
                mBattIcon.setImageResource(R.drawable.battery_3);
                break;
            case 4:
                mBattIcon.setImageResource(R.drawable.battery_4);
                break;
            case 5:
                mBattIcon.setImageResource(R.drawable.battery_5);
                break;
            }
            mbattery_status.setText(
                    (temperature / 10) + "C  " + getResources().getStringArray(R.array.batt_status)[status]);
        }
    };
    //getActivity().registerReceiver(batteryInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED) );
    setRetainInstance(true);
    setHasOptionsMenu(true);

}

From source file:edu.utexas.quietplaces.services.PlacesUpdateService.java

/**
 * Returns battery status. True if less than 10% remaining.
 *
 * @param battery Battery Intent/* w  w w .j  av  a2 s. c  o m*/
 * @return Battery is low
 */
protected boolean getIsLowBattery(Intent battery) {
    float pctLevel = (float) battery.getIntExtra(BatteryManager.EXTRA_LEVEL, 1)
            / battery.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
    return pctLevel < 0.15;
}