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

public void getCurrentStatus(final Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    assert batteryStatus != null;

    level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    health = 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);
}

From source file:fr.free.coup2lapan.ActualStateActivity.java

public BatteryStat getBatteryStatus() {

    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent status = this.registerReceiver(null, ifilter);

    BatteryStat batterystat = new BatteryStat(status.getIntExtra(BatteryManager.EXTRA_LEVEL, 0),
            status.getIntExtra(BatteryManager.EXTRA_SCALE, 0),
            status.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0),
            status.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0),
            status.getIntExtra(BatteryManager.EXTRA_STATUS, 0),
            status.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0),
            status.getIntExtra(BatteryManager.EXTRA_HEALTH, 0),
            status.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT) ? 1 : 0,
            DateFormat.getDateInstance(2, localeFR).format(new Date())
                    + DateFormat.getTimeInstance(2, localeFR).format(new Date()),
            status.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY));

    return batterystat;
}

From source file:us.dustinj.locationstore.LocationService.java

private float getBatteryPercent() {
    Intent batteryStatus = this.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

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

    return level / (float) scale * 100;
}

From source file:uk.ac.ucl.excites.sapelli.shared.util.android.DeviceControl.java

/**
 * Returns the current battery level as a percentage.
 * // www . j a  v a 2  s  . c o  m
 * @param context
 * @return
 */
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 just in case.
    if (level == -1 || scale == -1) {
        return 50.0f;
    }

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

From source file:zarbel.bat.services.BatteryThread.java

/**.drawable.ic_stat_name
 * Setting the battery percentage level to the notification
 * @param batteryStatusIntent/*from w  ww .ja  v  a2  s.  co  m*/
 * @param level
 * @param scale.drawable.ic_stat_name
 * @param mBuildewr
 * @param mNotificationManager
 * @return
 */
private void setBatteryPercentage(Context applicationContext, IntentFilter ifilter, Intent batteryStatusIntent,
        NotificationCompat.Builder mBuilder, NotificationManager mNotificationManager) {

    int level = 0; //Level data, got from batteryStatusIntent
    int scale = 0; //Scale level, got from batteryStatusIntent
    float totalLevel = 0; //Total battery level

    //Getting battery level
    batteryStatusIntent = applicationContext.registerReceiver(null, ifilter);
    level = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    scale = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    //Calculating battery level.
    totalLevel = level / (float) scale;
    int percent = ((int) (totalLevel * 100));

    //Creating and Showing notification

    if (notification == null)
        createNotification();

    //Getting the corresponding image with the right battery digit.
    int idResource = applicationContext.getResources().getIdentifier("ic_state_battery" + percent, "drawable",
            applicationContext.getPackageName());

    //Updating icon
    notification.icon = idResource;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB)
        notification.largeIcon = BitmapFactory.decodeResource(applicationContext.getResources(), idResource);

}

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 {//  ww  w. j  ava  2 s  .  c  o  m
        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.
 *///w  ww  . jav  a  2 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.
 *//*  w  w w .j  av a2 s  .c  o m*/
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.
 *///  ww w.  j a va2  s . 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;
    }//w  ww .j ava  2 s.co  m

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