Example usage for android.os BatteryManager BATTERY_STATUS_CHARGING

List of usage examples for android.os BatteryManager BATTERY_STATUS_CHARGING

Introduction

In this page you can find the example usage for android.os BatteryManager BATTERY_STATUS_CHARGING.

Prototype

int BATTERY_STATUS_CHARGING

To view the source code for android.os BatteryManager BATTERY_STATUS_CHARGING.

Click Source Link

Usage

From source file:org.proninyaroslav.libretorrent.core.utils.Utils.java

public static boolean isBatteryCharging(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}

From source file:vn.cybersoft.obs.android.fragments.BatteryInfoFragment.java

private void updateLayout() {
    mTemperature.setText((float) mBatteryInfo.temperature / 10 + "");
    mVoltage.setText((float) mBatteryInfo.voltage / 1000 + "");

    int batteryPercentage = mBatteryInfo.getBatteryPercentage();
    boolean isCharging = mBatteryInfo.status == BatteryManager.BATTERY_STATUS_CHARGING;
    mBatteryLevel.setText(batteryPercentage + "%");

    if (isCharging) {
        mTimeLeftText.setText(getString(R.string.charging_time_left));
        if (batteryPercentage <= 10) {
            mBatteryImg.setImageResource(R.drawable.psac0);
        } else if (batteryPercentage <= 25) {
            mBatteryImg.setImageResource(R.drawable.psac1);
        } else if (batteryPercentage <= 50) {
            mBatteryImg.setImageResource(R.drawable.psac2);
        } else if (batteryPercentage <= 75) {
            mBatteryImg.setImageResource(R.drawable.psac3);
        } else {/*from ww  w.  j a v a  2  s  .  c o m*/
            mBatteryImg.setImageResource(R.drawable.psac4);
        }
    } else {
        mTimeLeftText.setText(getString(R.string.time_left));
        if (batteryPercentage <= 10) {
            mBatteryImg.setImageResource(R.drawable.p1);
        } else if (batteryPercentage <= 25) {
            mBatteryImg.setImageResource(R.drawable.p2);
        } else if (batteryPercentage <= 50) {
            mBatteryImg.setImageResource(R.drawable.p3);
        } else if (batteryPercentage <= 75) {
            mBatteryImg.setImageResource(R.drawable.p4);
        } else {
            mBatteryImg.setImageResource(R.drawable.p5);
        }
    }
}

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

/**
 * Conversion from charging status int to String can be done through this method.
 *
 * @param status integer representing the charging status.
 * @return String representing the charging status.
 *///from ww  w  .jav  a  2s . c  om
private String getStatus(int status) {
    String statusString = UNKNOWN;
    switch (status) {
    case BatteryManager.BATTERY_STATUS_CHARGING:
        statusString = CHARGING;
        break;
    case BatteryManager.BATTERY_STATUS_DISCHARGING:
        statusString = DISCHARGING;
        break;
    case BatteryManager.BATTERY_STATUS_FULL:
        statusString = FULL;
        break;
    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
        statusString = NOT_CHARGING;
        break;
    }
    return statusString;
}

From source file:com.google.code.twisty.Twisty.java

private void appendBatteryState(StringBuffer sb) {
    IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent intent = registerReceiver(null, battFilter);

    int rawlevel = intent.getIntExtra("level", -1);
    int scale = intent.getIntExtra("scale", -1);
    int status = intent.getIntExtra("status", -1);
    int health = intent.getIntExtra("health", -1);
    int level = -1; // percentage, or -1 for unknown
    if (rawlevel >= 0 && scale > 0) {
        level = (rawlevel * 100) / scale;
    }// w ww . j a v a2  s.  c  o  m
    sb.append("The device");
    if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health) {
        sb.append("'s battery feels very hot!");
    } else {
        switch (status) {
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            // old emulator; maybe also when plugged in with no battery
            sb.append(" has no battery.");
            break;
        case BatteryManager.BATTERY_STATUS_CHARGING:
            sb.append("'s battery");
            if (level <= 33)
                sb.append(" is charging, and really ought to " + "remain that way for the time being.");
            else if (level <= 84)
                sb.append(" charges merrily.");
            else
                sb.append(" will soon be fully charged.");
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            if (level == 0)
                sb.append(" needs charging right away.");
            else if (level > 0 && level <= 33)
                sb.append(" is about ready to be recharged.");
            else
                sb.append("'s battery discharges merrily.");
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            sb.append(" is fully charged up and ready to go on " + "an adventure of some sort.");
            break;
        default:
            sb.append("'s battery is indescribable!");
            break;
        }
    }
    sb.append(" ");
}

From source file:com.tomi.ginatask.MainActivity.java

private void showDeviceInfoDialog() {
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = this.registerReceiver(null, intentFilter);

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

    float batteryPct = (level / (float) scale) * 100;

    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
            || status == BatteryManager.BATTERY_STATUS_FULL;

    builder.setTitle("Device info")
            .setMessage("IMEI: " + telephonyManager.getDeviceId() + "\n\nBattery level: " + batteryPct + "% "
                    + (isCharging ? "(charging)" : "(not charging)"))
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }//from ww w .j  av a2 s  .com
            }).show();
}

From source file:com.mozilla.SUTAgentAndroid.SUTAgentAndroid.java

private void monitorBatteryState() {
    battReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            StringBuilder sb = new StringBuilder();

            int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); // charge level from 0 to scale inclusive
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); // Max value for charge level
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
            boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); //0 if the device is not plugged in; 1 if plugged into an AC power adapter; 2 if plugged in via USB.
            //                int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); // voltage in millivolts
            nBatteryTemp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); // current battery temperature in tenths of a degree Centigrade
            //                String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);

            nChargeLevel = -1; // percentage, or -1 for unknown
            if (rawlevel >= 0 && scale > 0) {
                nChargeLevel = (rawlevel * 100) / scale;
            }// w ww. j  a v a2 s  .c  o m

            if (plugged > 0)
                sACStatus = "ONLINE";
            else
                sACStatus = "OFFLINE";

            if (present == false)
                sb.append("NO BATTERY");
            else {
                if (nChargeLevel < 10)
                    sb.append("Critical");
                else if (nChargeLevel < 33)
                    sb.append("LOW");
                else if (nChargeLevel > 80)
                    sb.append("HIGH");
            }

            if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health) {
                sb.append("Overheated ");
                sb.append((((float) (nBatteryTemp)) / 10));
                sb.append("(C)");
            } else {
                switch (status) {
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    // old emulator; maybe also when plugged in with no battery
                    if (present == true)
                        sb.append(" UNKNOWN");
                    break;
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    sb.append(" CHARGING");
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    sb.append(" DISCHARGING");
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    sb.append(" NOTCHARGING");
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    sb.append(" FULL");
                    break;
                default:
                    if (present == true)
                        sb.append("Unknown");
                    break;
                }
            }

            sPowerStatus = sb.toString();
        }
    };

    IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(battReceiver, battFilter);
}

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:com.intel.xdk.device.Device.java

private void registerBatteryChangeReceiver() {

    if (batteryChangeReceiver == null) {

        batteryChangeReceiver = new BroadcastReceiver() {

            @Override/*from  ww  w . j  av  a2 s .c  om*/
            public void onReceive(Context context, Intent intent) {
                int batteryStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                boolean isCharging = batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING
                        || batteryStatus == BatteryManager.BATTERY_STATUS_FULL;

                if (isCharging) {
                    if (wl == null) {
                        aquireWakeLock();
                    }
                } else {
                    if (wl != null) {
                        wl.release();
                        wl = null;
                    }
                }
            }

        };
    }

    activity.registerReceiver(batteryChangeReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}