zoukankan      html  css  js  c++  java
  • 充电状态和BatteryManager

    我们可以通过如下代码获取现在的充电状态是否为AC充电。

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = this.registerReceiver(null, filter);(http://www.amjmh.com/v/BIBRGZ_558768/)
    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
    if (acCharge) {
    Log.v(LOG_TAG,“The phone is charging!”);
    }
    1
    2
    3
    4
    5
    6
    7
    得到充电的状态之后我们可以有针对性的做一些操作,比如在充电时执行一些耗时的代码。

    private boolean checkForPower() {
    // It is very easy to subscribe to changes to the battery state, but you can get the current
    // state by simply passing null in as your receiver. Nifty, isn't that?
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = this.registerReceiver(null, filter);

    // There are currently three ways a device can be plugged in. We should check them all.
    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);
    }
    return (usbCharge || acCharge || wirelessCharge);
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    上面代码可以得到现在是否充电。
    ————————————————

  • 相关阅读:
    关于ping github.com超时的解决办法
    git使用过程中的若干问题笔记
    PAT甲级1017题解——模拟排序
    第七章4
    第七章3
    第七章2
    第七章1
    第六章4
    第六章3
    第六章2
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11408516.html
Copyright © 2011-2022 走看看