Java Code Examples for android.os.BatteryManager#BATTERY_STATUS_CHARGING

The following examples show how to use android.os.BatteryManager#BATTERY_STATUS_CHARGING . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatteryStatusView.java    From VIA-AI with MIT License 6 votes vote down vote up
public void updateBatteryStatus(Context context) {
    IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, iFilter);

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


    Integer status = null;
    if (batteryStatus != null) {
        status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        mIsCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING);

    }

    mBatteryPercentage = (int)((level / (float) scale) *100);
}
 
Example 2
Source File: BatteryChangeReceiver.java    From CacheEmulatorChecker with Apache License 2.0 6 votes vote down vote up
@Override
    public void onReceive(Context context, Intent intent) {


        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
        mCurrentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

        switch (status) {

            case BatteryManager.BATTERY_STATUS_CHARGING:
                // 电池满不作为参考,看做充电中
            case BatteryManager.BATTERY_STATUS_FULL:
            case BatteryManager.BATTERY_STATUS_UNKNOWN:
                mIsCharging = true;
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                mIsCharging = false;
                break;
        }

//        Toast.makeText(context, " " + mCurrentLevel + " mIsCharging "+ mIsCharging, Toast.LENGTH_SHORT).show();
    }
 
Example 3
Source File: ChargingStateReceiver.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
public static EventChargingState grabChargingState(Context context) {
    BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);

    if (bm == null)
        return new EventChargingState(false);

    int status = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
            || status == BatteryManager.BATTERY_STATUS_FULL;

    EventChargingState event = new EventChargingState(isCharging);
    return event;
}
 
Example 4
Source File: UENavigationActivity.java    From Auie with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	int level = intent.getIntExtra("level", -1);
          int status = intent.getIntExtra("status", -1);
          switch (status) {
	case BatteryManager.BATTERY_STATUS_FULL:
		mNavigationView.setStatus(UIBatteryView.STATUS_COMLETED);
		mNavigationView.setBatteryText("已充满");
		break;
	case BatteryManager.BATTERY_STATUS_CHARGING:
		mNavigationView.setStatus(UIBatteryView.STATUS_CHARGED);
		mNavigationView.setBatteryText("充电中");
		break;
	case BatteryManager.BATTERY_STATUS_DISCHARGING:
	case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
		mNavigationView.setLevel(level * 0.01f);
		mNavigationView.setBatteryText(level + "%");
		break;
	default:
		mNavigationView.setLevel(0);
		mNavigationView.setBatteryText("无电池");
		break;
	}
}
 
Example 5
Source File: BatteryService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Synchronize on BatteryService.
 */
public void updateLightsLocked() {
    final int level = mHealthInfo.batteryLevel;
    final int status = mHealthInfo.batteryStatus;
    if (level < mLowBatteryWarningLevel) {
        if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
            // Solid red when battery is charging
            mBatteryLight.setColor(mBatteryLowARGB);
        } else {
            // Flash red when battery is low and not charging
            mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
                    mBatteryLedOn, mBatteryLedOff);
        }
    } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
            || status == BatteryManager.BATTERY_STATUS_FULL) {
        if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
            // Solid green when full or charging and nearly full
            mBatteryLight.setColor(mBatteryFullARGB);
        } else {
            // Solid orange when charging and halfway full
            mBatteryLight.setColor(mBatteryMediumARGB);
        }
    } else {
        // No lights if not charging and not low
        mBatteryLight.turnOff();
    }
}
 
Example 6
Source File: OfflinePageUtils.java    From delion with Apache License 2.0 5 votes vote down vote up
private static boolean isPowerConnected(Intent batteryStatus) {
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isConnected = (status == BatteryManager.BATTERY_STATUS_CHARGING
            || status == BatteryManager.BATTERY_STATUS_FULL);
    Log.d(TAG, "Power connected is " + isConnected);
    return isConnected;
}
 
Example 7
Source File: ChargingStatusController.java    From Trigger with Apache License 2.0 5 votes vote down vote up
@Override
    public void parseStatus(Intent data) {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, filter);
        if (batteryStatus != null) {
            int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
                    || status == BatteryManager.BATTERY_STATUS_FULL;
//            int chargePlug = data.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
//            boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
//            boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
            DeviceStatus.chargingConstraintSatisfied.set(isCharging);
        }
    }
 
Example 8
Source File: BatteryTileService.java    From SystemUITuner2 with MIT License 5 votes vote down vote up
private void setBat(Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

        mIsCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

//        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        mBatteryPercent = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

        Tile battery = getQsTile();

        int resId;

        if (mBatteryPercent >= 95) {
             resId = mIsCharging ? R.drawable.ic_battery_charging_full : R.drawable.ic_battery;
        } else if (mBatteryPercent >= 85) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_90 : R.drawable.ic_battery_90;
        } else if (mBatteryPercent >= 70) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_80 : R.drawable.ic_battery_80;
        } else if (mBatteryPercent >= 55) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_60 : R.drawable.ic_battery_60;
        } else if (mBatteryPercent >= 40) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_50 : R.drawable.ic_battery_50;
        } else if (mBatteryPercent >= 25) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_30 : R.drawable.ic_battery_30;
        } else if (mBatteryPercent >= 15) {
            resId = mIsCharging ? R.drawable.ic_battery_charging_20 : R.drawable.ic_battery_20;
        } else {
            resId = mIsCharging ? R.drawable.ic_battery_charging_20 : R.drawable.ic_battery_alert;
        }

        battery.setIcon(Icon.createWithResource(this, resId));
        battery.setLabel(String.valueOf(mBatteryPercent).concat("%"));
        battery.updateTile();
    }
 
Example 9
Source File: NeihanVideoPlayerController.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
            BatteryManager.BATTERY_STATUS_UNKNOWN);
    if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
        // 充电中
        mBattery.setImageResource(R.drawable.battery_charging);
    } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
        // 充电完成
        mBattery.setImageResource(R.drawable.battery_full);
    } else {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
        int percentage = (int) (((float) level / scale) * 100);
        if (percentage <= 10) {
            mBattery.setImageResource(R.drawable.battery_10);
        } else if (percentage <= 20) {
            mBattery.setImageResource(R.drawable.battery_20);
        } else if (percentage <= 50) {
            mBattery.setImageResource(R.drawable.battery_50);
        } else if (percentage <= 80) {
            mBattery.setImageResource(R.drawable.battery_80);
        } else if (percentage <= 100) {
            mBattery.setImageResource(R.drawable.battery_100);
        }
    }
}
 
Example 10
Source File: PieController.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
public String getBatteryLevel() {
    if (mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
        return mGbResources.getString(R.string.pie_battery_status_full);
    }
    if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
        return mGbResources.getString(R.string.pie_battery_status_charging, mBatteryLevel);
    }
    return mGbResources.getString(R.string.pie_battery_status_discharging, mBatteryLevel);
}
 
Example 11
Source File: Utils.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
public static boolean checkIfCharging() {
	try {
		IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent batteryStatus = FlickrUploader.getAppContext().registerReceiver(null, ifilter);
		int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
		boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
		setCharging(isCharging);
	} catch (Throwable e) {
		LOG.error(ToolString.stack2string(e));
	}
	return charging;
}
 
Example 12
Source File: TaskbarController.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
private Drawable getBatteryDrawable() {
    BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
    int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

    if(batLevel == Integer.MIN_VALUE)
        return null;

    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);

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

    String batDrawable;
    if(batLevel < 10 && !isCharging)
        batDrawable = "alert";
    else if(batLevel < 25)
        batDrawable = "20";
    else if(batLevel < 40)
        batDrawable = "30";
    else if(batLevel < 55)
        batDrawable = "50";
    else if(batLevel < 70)
        batDrawable = "60";
    else if(batLevel < 85)
        batDrawable = "80";
    else if(batLevel < 95)
        batDrawable = "90";
    else
        batDrawable = "full";

    String charging;
    if(isCharging)
        charging = "charging_";
    else
        charging = "";

    String batRes = "tb_battery_" + charging + batDrawable;
    int id = getResourceIdFor(batRes);

    return getDrawableForSysTray(id);
}
 
Example 13
Source File: Requirements.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private boolean checkChargingRequirement(Context context) {
  if (!isChargingRequired()) {
    return true;
  }
  Intent batteryStatus =
      context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  if (batteryStatus == null) {
    return false;
  }
  int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  return status == BatteryManager.BATTERY_STATUS_CHARGING
      || status == BatteryManager.BATTERY_STATUS_FULL;
}
 
Example 14
Source File: BatteryUtils.java    From SimpleSmsRemote with MIT License 5 votes vote down vote up
/**
 * check if battery is charging
 *
 * @param context app context
 * @return true if battery is charging
 * @throws Exception
 */
public static boolean IsBatteryCharging(Context context) throws Exception {
    Intent batteryStatus = getBatteryStatusIntent(context);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    if (status == -1)
        throw new Exception("failed to get battery status extra");

    return status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;
}
 
Example 15
Source File: SystemUtil.java    From KeyboardView with Apache License 2.0 5 votes vote down vote up
/**
 * 判断手机当前是否在充电状态
 *
 * @param context
 * @return
 */
public static boolean isBatteryCharging(Context context) {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, filter);

    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    if ((status == BatteryManager.BATTERY_STATUS_CHARGING) || (status == BatteryManager.BATTERY_STATUS_FULL)) {
        return true;
    }
    return false;
}
 
Example 16
Source File: SensorReader.java    From homeDash with Apache License 2.0 5 votes vote down vote up
public  void getBatteryReading(SensorDataListener listener){

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

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

        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

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

        float batteryPct = level / (float)scale;

        Log.i(TAG, "AC connected: "+acCharge);
        Log.i(TAG, "USB connected: "+usbCharge);
        Log.i(TAG, "Battery charging: "+ isCharging);
        Log.i(TAG, "Battery Level: "+ batteryPct);

        ArrayMap<String, String> map = new ArrayMap<>(3);
        map.put(SENSOR, "Battery");
        map.put(VALUE, Integer.toString(level));
        map.put(UNIT, BATTERYSENSOR_UNIT);
        map.put("charging", Boolean.toString(isCharging));
        map.put("acPlugged", Boolean.toString(acCharge));
        map.put("usbPlugged", Boolean.toString(usbCharge));
        listener.sensorData(map);
    }
 
Example 17
Source File: XndroidReceiver.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
private void handleBattery(Intent intent){
    int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
    int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
    int levelPercent = (int)(((float)level / scale) * 100);
    boolean charging = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
            BatteryManager.BATTERY_STATUS_UNKNOWN) == BatteryManager.BATTERY_STATUS_CHARGING;
    if(levelPercent < BATTERY_LOW_LIMIT && !charging)
        AppModel.sDevBatteryLow = true;
    else
        AppModel.sDevBatteryLow = false;
    LogUtils.i("battery:" + level +"%, BatteryLow=" + AppModel.sDevBatteryLow);
}
 
Example 18
Source File: SystemConfig.java    From AndroidDownload with Apache License 2.0 4 votes vote down vote up
public Boolean getSystemBatteryStatus() {
    batteryInfoIntent = x.app().getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int state = batteryInfoIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    return (state == BatteryManager.BATTERY_STATUS_CHARGING);
}
 
Example 19
Source File: LockDeviceService.java    From SecondScreen with Apache License 2.0 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    super.onHandleIntent(intent);

    // Close the notification drawer
    Intent closeDrawer = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    sendBroadcast(closeDrawer);

    // Determine current charging status
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = registerReceiver(null, ifilter);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;

    // Get current UI mode
    UiModeManager mUiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
    int uiMode = mUiModeManager.getCurrentModeType();

    // Determine current dock state, based on the current UI mode
    boolean isDocked;
    switch(uiMode) {
        case Configuration.UI_MODE_TYPE_DESK:
            isDocked = true;
            break;
        case Configuration.UI_MODE_TYPE_CAR:
            isDocked = true;
            break;
        default:
            isDocked = false;
    }

    // In order to ensure that the device locks itself when the following code is run,
    // we need to temporarily set the lock screen lock after timeout value.
    // For a smooth transition into the daydream, we set this value to one millisecond,
    // locking the device at the soonest opportunity after the transition completes.
    int timeout = Settings.Secure.getInt(getContentResolver(), "lock_screen_lock_after_timeout", 5000);
    if(timeout != 1) {
        SharedPreferences prefMain = U.getPrefMain(this);
        SharedPreferences.Editor editor = prefMain.edit();
        editor.putInt("timeout", Settings.Secure.getInt(getContentResolver(), "lock_screen_lock_after_timeout", 5000));
        editor.apply();

        U.runCommand(this, U.timeoutCommand + "1");
    }

    // Schedule TimeoutService to reset lock screen timeout to original value
    Intent timeoutService = new Intent(this, TimeoutService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 123456, timeoutService, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent);

    // If Daydreams is enabled and the device is charging, then lock the device by launching the daydream.
    if(isCharging
            && !U.castScreenActive(this)
            && Settings.Secure.getInt(getContentResolver(), "screensaver_enabled", 0) == 1
            && ((Settings.Secure.getInt(getContentResolver(), "screensaver_activate_on_dock", 0) == 1 && isDocked)
            || Settings.Secure.getInt(getContentResolver(), "screensaver_activate_on_sleep", 0) == 1)) {
        // Send intent to launch the current daydream manually
        Intent lockIntent = new Intent(Intent.ACTION_MAIN);
        lockIntent.setComponent(ComponentName.unflattenFromString("com.android.systemui/.Somnambulator"));
        lockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            startActivity(lockIntent);
        } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
    } else
        // Otherwise, send a power button keystroke to lock the device normally
        U.lockDevice(this);
}
 
Example 20
Source File: BatteryInformation.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Method to resolve the battery status
 */
private void getStatus() {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getStatus");
    }

    if (batteryIntent != null) {

        final SaiyResources sr = new SaiyResources(mContext, sl);

        final int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
                BatteryManager.BATTERY_STATUS_UNKNOWN);

        switch (status) {

            case BatteryManager.BATTERY_STATUS_CHARGING:

                int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
                        BatteryManager.BATTERY_STATUS_UNKNOWN);

                switch (plugged) {
                    case BatteryManager.BATTERY_PLUGGED_AC:
                        setStatusResponse(sr.getString(ai.saiy.android.R.string.ac_charging));
                        break;
                    case BatteryManager.BATTERY_PLUGGED_USB:
                        setStatusResponse(sr.getString(ai.saiy.android.R.string.usb_charging));
                        break;
                    default:
                        setStatusResponse(sr.getString(ai.saiy.android.R.string.charging));
                        break;
                }
                break;

            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.discharging));
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.discharging));
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.fully_charged));
                break;
            case BatteryManager.BATTERY_STATUS_UNKNOWN:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.currently_indeterminable));
                break;
            default:
                setStatusResponse(sr.getString(ai.saiy.android.R.string.currently_indeterminable));
                break;
        }

        sr.reset();

    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "batteryIntent: null");
        }
        setAccessFailure();
    }
}