2015-01-17 89 views
1

我試過了Android的powerprofile ....我試過這個代碼...但它給了我每次1000個答案在所有設備中... 有沒有其他方法在android系統獲得電池容量... Eg.if移動設備容量2000mAh的是,它應該返回我2000以mAh爲單位的電池總容量以編程方式

public Double getBatteryCapacity() { 

     Object mPowerProfile_ = null; 
     double batteryCapacity = 0; 
     final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile"; 
     try { 
      mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS) 
        .getConstructor(Context.class).newInstance(this); 

     } catch (Exception e) { 

      // Class not found? 
      e.printStackTrace(); 
     } 

     try { 

      // Invoke PowerProfile method "getAveragePower" with param 
      // "battery.capacity" 
      batteryCapacity = (Double) Class.forName(POWER_PROFILE_CLASS) 
        .getMethod("getAveragePower", java.lang.String.class) 
        .invoke(mPowerProfile_, "battery.capacity"); 

     } catch (Exception e) { 

      // Something went wrong 
      e.printStackTrace(); 
     } 

     return batteryCapacity; 
    } 
+0

可能重複以mA爲單位的電流容量和以mAh爲單位的電池總容量](http://stackoverflow.com/questions/23193388/android-get-battery-current-capacity-in-ma-and-total-capacity-of-battery-in -mah) – Psypher

+1

Ranjith,但它不工作,這就是爲什麼我發佈這...你能幫我嗎? –

+0

[Android的有可能重複獲取設備的電池容量?](http://stackoverflow.com/questions/22243461/android-is-there-anyway-to-get-battery-capacity-它只適用於android 5.0而不適用於較低版本。 –

回答

0

可以使用android.os.BatteryManager的下列屬性。 BATTERY_PROPERTY_CHARGE_COUNTER以微安時爲單位提供剩餘電池容量。 BATTERY_PROPERTY_CAPACITY,它以剩下的電池容量爲整數百分比。

所以,

BATTERY_PROPERTY_CHARGE_COUNTER/BATTERY_PROPERTY_CAPACITY * 100

會給你總電池容量。這不是精確的計算,但您可以關閉實際容量。

參考文獻: https://source.android.com/devices/tech/power/index.html#device-power http://developer.android.com/reference/android/os/BatteryManager.html

+0

它僅適用於android 5.0,不適用於較低版本。 –

1

對於有意的@Yehan建議的執行的那些用戶,這裏是返回的總電池容量的方法,包括:(僅適用於API級別> = 21)

public long getBatteryCapacity(Context context) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     BatteryManager mBatteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); 
     Integer chargeCounter = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER); 
     Integer capacity = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); 

     if(chargeCounter == Integer.MIN_VALUE || capacity == Integer.MIN_VALUE) 
      return 0; 

     return (chargeCounter/capacity) *100; 
    } 
    return 0; 
} 

不幸的是,由於某種原因,這種方法並不總是奏效。在這種情況下,你可以使用Java的反射API的中檢索由com.android.internal.os.PowerProfile的getBatteryCapacity方法的返回值:

public double getBatteryCapacity(Context context) { 
    Object mPowerProfile; 
    double batteryCapacity = 0; 
    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile"; 

    try { 
     mPowerProfile = Class.forName(POWER_PROFILE_CLASS) 
       .getConstructor(Context.class) 
       .newInstance(context); 

     batteryCapacity = (double) Class 
       .forName(POWER_PROFILE_CLASS) 
       .getMethod("getBatteryCapacity") 
       .invoke(mPowerProfile); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return batteryCapacity; 

} 

來源: https://android.googlesource.com/platform/frameworks/base/+/a029ea1/core/java/com/android/internal/os/PowerProfile.java

[Android開電池
相關問題