Example usage for android.os BatteryManager EXTRA_PLUGGED

List of usage examples for android.os BatteryManager EXTRA_PLUGGED

Introduction

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

Prototype

String EXTRA_PLUGGED

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

Click Source Link

Document

Extra for android.content.Intent#ACTION_BATTERY_CHANGED : integer indicating whether the device is plugged in to a power source; 0 means it is on battery, other constants are different types of power sources.

Usage

From source file:Main.java

public static int getChargingMode(Intent batteryStatus) {
    return batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
}

From source file:Main.java

public static boolean isCharging(Context context) {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, filter);
    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
    boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
    boolean wirelessCharge = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS);
    }/*from w ww . j av  a2  s. c o m*/
    return (usbCharge || acCharge || wirelessCharge);
}

From source file:Main.java

public static boolean isPowerConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    if (intent == null) {
        return true;
    }// w ww . j  a v a  2s .c  om
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB
            || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}

From source file:Main.java

/**
 * Returns true if the charger is currently connected
 * @param context/*from   w  w w  .  j a va2 s .  co  m*/
 * @return true if the charger is connected
 */
public static boolean isConnected(Context context) {
    // make a synchronous call
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

From source file:Main.java

/**
 * @return true is device is plugged at this moment, false otherwise.
 * @see #isPlugged(android.content.Context)
 *//* ww  w. ja v a 2s  . com*/
@SuppressLint("InlinedApi")
public static boolean isPlugged(@Nullable Intent intent) {
    if (intent == null) {
        return false;
    }

    final int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB
            || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}

From source file:siarhei.luskanau.gps.tracker.free.utils.Utils.java

public static boolean isPowerConnected(Context context) {
    Intent intent = context.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

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 v a  2  s .  co  m*/
    }

    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:rus.cpuinfo.AndroidDepedentModel.BatteryInfo.java

@NonNull
private String getBatteryPowerSource() {

    return getBatteryInfo(BatteryManager.EXTRA_PLUGGED);

}

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 ww w.  jav a2 s  . c  o m*/
    }

    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();/* ww w  .j  a  v  a2  s.c o m*/
    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);
}