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:com.hmatalonga.greenhub.managers.sampling.DataEstimator.java

public void getCurrentStatus(final Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

    try {//from w w w. j ava  2s .  c  om
        Intent batteryStatus = context.registerReceiver(null, ifilter);

        if (batteryStatus != null) {
            level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            mHealth = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
            plugged = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            present = batteryStatus.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
            status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            technology = batteryStatus.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
            temperature = (float) (batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10);
            voltage = (float) (batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0) / 1000);
        }
    } catch (ReceiverCallNotAllowedException e) {
        LOGE(TAG, "ReceiverCallNotAllowedException from Notification Receiver?");
        e.printStackTrace();
    }
}

From source file:count.ly.messaging.CrashDetails.java

/**
 * Returns the current device battery level.
 *///  ww w .  ja  va2 s  .  c  o m
static String getBatteryLevel(Context context) {
    try {
        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 > 0) {
            return Float.toString(((float) level / (float) scale) * 100.0f);
        }
    } catch (Exception e) {
        if (Countly.sharedInstance().isLoggingEnabled()) {
            Log.i(Countly.TAG, "Can't get batter level");
        }
    }

    return null;
}

From source file:org.wso2.emm.agent.api.DeviceState.java

/**
 * Returns the device battery information.
 *
 * @return Battery object representing battery data.
 *///from w w w . j av a  2 s. c om
public Power getBatteryDetails() {
    Power power = new Power();
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = 0;
    int scale = 0;
    int plugState = 0;
    int healthState = 0;
    if (batteryIntent != null) {
        level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, DEFAULT_LEVEL);
        scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, DEFAULT_LEVEL);
        plugState = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, DEFAULT_LEVEL);
        healthState = batteryIntent.getIntExtra(BatteryManager.EXTRA_HEALTH, DEFAULT_LEVEL);
    }

    power.setLevel(level);
    power.setScale(scale);
    power.setPlugged(getPlugType(plugState));
    power.setHealth(getHealth(healthState));
    return power;
}

From source file:ly.count.android.sdk.CrashDetails.java

/**
 * Returns the current device battery level.
 *//*from  w w  w. ja  va2s.  c  om*/
static String getBatteryLevel(Context context) {
    try {
        Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        if (batteryIntent != null) {
            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 > 0) {
                return Float.toString(((float) level / (float) scale) * 100.0f);
            }
        }
    } catch (Exception e) {
        if (Countly.sharedInstance().isLoggingEnabled()) {
            Log.i(Countly.TAG, "Can't get batter level");
        }
    }

    return null;
}

From source file:org.proninyaroslav.libretorrent.core.utils.Utils.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 ww . j  a v a  2 s . com*/

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

From source file:org.protocoderrunner.apprunner.api.PDevice.java

@ProtoMethod(description = "", example = "")
@ProtoMethodParam(params = { "" })
public void battery(final StartBateryListenerCB cb) {
    WhatIsRunning.getInstance().add(this);
    batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        boolean isConnected = false;
        private int status;
        private final boolean alreadyKilled = false;

        @Override//from w ww. j a va2 s.  co m
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            // isCharging =
            // intent.getBooleanExtra(BatteryManager.EXTRA_PLUGGED, false);
            // status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

            if (status == BatteryManager.BATTERY_PLUGGED_AC) {
                isConnected = true;
            } else if (status == BatteryManager.BATTERY_PLUGGED_USB) {
                isConnected = true;
            } else {
                isConnected = false;
            }

            BatteryReturn o = new BatteryReturn();

            o.level = level;
            o.temperature = temp;
            o.connected = isConnected;

            // plugConnected = isConnected;
            cb.event(o);
            Log.d("BATTERY", "level is " + level + " is connected " + isConnected);
        }
    };

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    getContext().registerReceiver(batteryReceiver, filter);
}

From source file:com.google.transporttracker.TrackerService.java

private float getBatteryLevel() {
    Intent batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int batteryLevel = -1;
    int batteryScale = 1;
    if (batteryStatus != null) {
        batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, batteryLevel);
        batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, batteryScale);
    }/*from  www  .j a  va2 s .c o m*/
    return batteryLevel / (float) batteryScale * 100;
}

From source file:org.protocoderrunner.apprunner.api.PDevice.java

@ProtoMethod(description = "Get the current device battery level", example = "")
@ProtoMethodParam(params = { "" })
public float battery() {
    Intent batteryIntent = getContext().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 ww  w.  j  a v a  2s .c o m

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

From source file:com.iss.android.wearable.datalayer.MainActivity.java

private void initializeSWBatteryChecker() {
    final TextView SWBatteryStatus = (TextView) findViewById(R.id.SWbatteryLabel);
    final Handler h = new Handler();
    final int delay = 20000; //milliseconds
    final boolean[] warned_evening = { false };

    // Timer's fine for Java, but kills android apps.

    h.postDelayed(new Runnable() {
        public void run() {
            IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent batteryStatus = registerReceiver(null, ifilter);

            int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

            int batteryPct = (int) (level / (float) scale * 100);
            SWBatteryStatus.setText("SW: " + batteryPct + "%");
            Calendar clnd = Calendar.getInstance();
            if (clnd.get(Calendar.HOUR_OF_DAY) >= 20) {
                if (batteryPct < 75 && !warned_evening[0]) {
                    warned_evening[0] = true;
                    displaySWBatteryWarning();
                } else if (batteryPct >= 75 && warned_evening[0]) {
                    warned_evening[0] = false;
                }//from  w w w. j a v a  2s.  c  om
            }
            h.postDelayed(this, delay);
        }
    }, 0);
}

From source file:com.thomasokken.free42.Free42Activity.java

@Override
protected void onResume() {
    super.onResume();

    // Check battery level -- this is necessary because the ACTTON_BATTERY_LOW
    // and ACTION_BATTERY_OKAY intents are not "sticky", i.e., we get those
    // notifications only when that status *changes*; we don't get any indication
    // of what that status *is* when the app is launched (or resumed?).
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = registerReceiver(null, ifilter);
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    if (low_battery)
        low_battery = level * 100 < scale * 20;
    else/*  w  ww  .  j  a  v a 2 s.  c  o  m*/
        low_battery = level * 100 <= scale * 15;
    Rect inval = skin.update_annunciators(-1, -1, -1, -1, low_battery ? 1 : 0, -1, -1);
    if (inval != null)
        calcView.postInvalidateScaled(inval.left, inval.top, inval.right, inval.bottom);

    if (core_powercycle())
        start_core_keydown();
}