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
    上面代码可以得到现在是否充电。
    ————————————————

  • 相关阅读:
    手机精准定位,看好你的男朋友
    顶级分享,三端看片的日子来了
    这样的声音谁受的了呀
    白嫖vip电台,资源随意听
    老板止步!这里全是小姐姐
    粉丝福利。无视墙 来看看全世界的网站吧
    利用自己的服务器搭建专属私有云盘&博客园搬家
    Photoshop 2020 安装教程
    TensorFlow基础——常用函数(一)
    Scala基本语法入门
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11408516.html
Copyright © 2011-2022 走看看