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:Main.java

public static boolean isCharging(Context context) {
    Intent batteryStatus = getBatteryStatus(context);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}

From source file:Main.java

private static Boolean isCharging(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

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

From source file:Main.java

public static boolean isChargingOrFull(Intent batteryStatus) {
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}

From source file:Main.java

public static boolean isCharging(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);

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

From source file:Main.java

public static boolean isPowerConnected(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

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

    return isCharging;
}

From source file:com.owncloud.android.utils.ConnectivityUtils.java

public static boolean isCharging(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);

    int status = 0;
    if (batteryStatus != null) {
        status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    }/*from ww  w  .  ja  va 2 s .co  m*/
    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}

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:fr.inria.ucn.collectors.SysStateCollector.java

/**
 * //ww  w.j av  a  2  s. c  om
 * @param c
 * @param ts
 * @param change
 */
@SuppressLint("NewApi")
public void run(Context c, long ts, boolean change) {
    try {
        JSONObject data = new JSONObject();
        data.put("on_screen_state_change", change); // this collection run was triggered by screen state change

        data.put("hostname", Helpers.getSystemProperty("net.hostname", "unknown hostname"));
        data.put("current_timezone", Time.getCurrentTimezone());

        // general memory state
        ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);

        JSONObject mem = new JSONObject();
        mem.put("available", mi.availMem);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            mem.put("total", mi.totalMem);
        }
        mem.put("is_low", mi.lowMemory);
        data.put("memory", mem);

        // screen state
        PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
        data.put("screen_on", pm.isScreenOn());

        // battery state
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent battery = c.registerReceiver(null, ifilter);
        int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        float pct = (float) (100.0 * level) / scale;
        int status = battery.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING
                || status == BatteryManager.BATTERY_STATUS_FULL);
        int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        JSONObject batt = new JSONObject();
        batt.put("level", level);
        batt.put("scale", scale);
        batt.put("pct", pct);
        batt.put("is_charging", isCharging);
        batt.put("usb_charge", usbCharge);
        batt.put("ac_charge", acCharge);
        data.put("battery", batt);

        // some proc stats
        data.put("cpu", getCpuStat());
        data.put("loadavg", getLoadStat());
        data.put("uptime", getUptimeStat());

        // audio state
        data.put("audio", getAudioState(c));

        // done
        Helpers.sendResultObj(c, "system_state", ts, data);

    } catch (JSONException jex) {
        Log.w(Constants.LOGTAG, "failed to create json object", jex);
    }
}

From source file:com.sssemil.advancedsettings.MainService.java

public static boolean isPhonePluggedIn(Context context) {
    boolean charging = false;

    final Intent batteryIntent = context.registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = 0;
    if (batteryIntent != null) {
        status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    }/*w  ww.ja  v  a  2s.  co  m*/
    boolean batteryCharge = status == BatteryManager.BATTERY_STATUS_CHARGING;

    int chargePlug = 0;
    if (batteryIntent != null) {
        chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    }
    boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

    if (batteryCharge)
        charging = true;
    if (usbCharge)
        charging = true;
    if (acCharge)
        charging = true;

    return charging;
}

From source file:org.opensilk.music.ui2.LauncherActivity.java

@Override
protected void onStart() {
    super.onStart();
    mPluginConnectionManager.onResume();
    if (RxUtils.notSubscribed(chargingSubscription)) {
        //check if already plugged first
        IntentFilter filter2 = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent battChanged = registerReceiver(null, filter2);
        int battStatus = (battChanged != null) ? battChanged.getIntExtra(BatteryManager.EXTRA_STATUS, -1) : -1;
        if (battStatus == BatteryManager.BATTERY_STATUS_CHARGING
                || battStatus == BatteryManager.BATTERY_STATUS_FULL) {
            keepScreenOn(true);//from  w  w  w. java  2s.c om
        }
        // keep apprised of future plug events
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        chargingSubscription = AndroidObservable.fromBroadcast(this, filter).subscribe(new Action1<Intent>() {
            @Override
            public void call(Intent intent) {
                if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
                    keepScreenOn(true);
                } else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
                    keepScreenOn(false);
                }
            }
        });
    }
}