如何在 Android 的后台监控电池电量?

Posted

技术标签:

【中文标题】如何在 Android 的后台监控电池电量?【英文标题】:How to monitor battery levels in the background on Android? 【发布时间】:2013-09-24 23:33:25 【问题描述】:

因此,对于我的应用,我想监控电池电量,即使我的应用已关闭,然后在电池电量低于某个百分比时发送通知。我知道如何创建像this 这样的后台服务,但我不确定如何使用它来监控电池电量。我会在 Manifest 文件中注册一个 BroadcastReceiver,还是应该动态地注册?另外,我担心 BroadcastReceiver 在收到第一次更新后会被操作系统终止。

【问题讨论】:

***.com/questions/24532934/… 看走路吃的解决办法 【参考方案1】:

您必须动态注册它。

在服务中这样做可能是一个不错的选择。这是我用过的:

this.registerReceiver(this.mBatInfoReceiver, 
                  new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

...

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()
    @Override
    public void onReceive(Context arg0, Intent intent) 
      // TODO Auto-generated method stub
        //this will give you battery current status

    try
      int level = intent.getIntExtra("level", 0);
      int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
      int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
      int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

      String BStatus = "No Data";
      if (status == BatteryManager.BATTERY_STATUS_CHARGING)BStatus = "Charging";
      if (status == BatteryManager.BATTERY_STATUS_DISCHARGING)BStatus = "Discharging";
      if (status == BatteryManager.BATTERY_STATUS_FULL)BStatus = "Full";
      if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING)BStatus = "Not Charging";
      if (status == BatteryManager.BATTERY_STATUS_UNKNOWN)BStatus = "Unknown";

      int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
      String BattPowerSource = "No Data";
      if (chargePlug == BatteryManager.BATTERY_PLUGGED_AC)BattPowerSource = "AC";
      if (chargePlug == BatteryManager.BATTERY_PLUGGED_USB)BattPowerSource = "USB";

      String BattLevel = String.valueOf(level);

      int BHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
      String BatteryHealth = "No Data";
      if (BHealth == BatteryManager.BATTERY_HEALTH_COLD)BatteryHealth = "Cold";
      if (BHealth == BatteryManager.BATTERY_HEALTH_DEAD)BatteryHealth = "Dead";
      if (BHealth == BatteryManager.BATTERY_HEALTH_GOOD)BatteryHealth = "Good";
      if (BHealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE)BatteryHealth = "Over-Voltage";
      if (BHealth == BatteryManager.BATTERY_HEALTH_OVERHEAT)BatteryHealth = "Overheat";
      if (BHealth == BatteryManager.BATTERY_HEALTH_UNKNOWN)BatteryHealth = "Unknown";
      if (BHealth == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE)BatteryHealth = "Unspecified Failure";

      //Do whatever with the data here


     catch (Exception e)
        Log.v(TAG, "Battery Info Error");
    
    
  ;

【讨论】:

你知道为什么我必须动态地做吗? developer.android.com/reference/android/os/BatteryManager.html - “您无法通过清单中声明的​​组件接收此信息,只能通过 Context.registerReceiver() 显式注册它。请参阅 ACTION_BATTERY_LOW、ACTION_BATTERY_OKAY、ACTION_POWER_CONNECTED 和 ACTION_POWER_DISCONNECTED 了解不同的电池 -通过清单接收器发送和接收的相关广播。" 此代码仅在我启动应用程序时运行一次......我需要它继续运行......如何做到这一点? 尝试使用AlarmManager

以上是关于如何在 Android 的后台监控电池电量?的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中获取电池电量变化(仅当有电量/百分比变化时)?

如何在android中的ble中每5秒更新一次电池电量

在android中自动更新电池电量

为啥我的 Android 应用程序偶尔会非常快地耗尽电池电量?

如何在运行时获取 Android 中的电池统计信息?

Android:有没有办法知道我们的应用程序消耗的电池电量?