2016-09-06 53 views
0

這段代碼給了我每個設備上的2000,所有以前關於這個問題的問題都給出了無關的答案。CPU-Z應用程序如何在Android中提供最大電池容量(以mAh爲單位)?

請別人幫忙

public void getBatteryCapacity() { 
    Object mPowerProfile_ = null; 

    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile"; 

    try { 
     mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS) 
       .getConstructor(Context.class).newInstance(getContext()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     double batteryCapacity = (Double) Class 
       .forName(POWER_PROFILE_CLASS) 
       .getMethod("getAveragePower", java.lang.String.class) 
       .invoke(mPowerProfile_, "battery.capacity"); 
     Toast.makeText(getActivity(), batteryCapacity + " mah", 
       Toast.LENGTH_LONG).show(); 
     Log.d("Capacity",batteryCapacity+" mAh"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

但我想最大容量,如CPU-Z應用程序使:

enter image description here

+0

用'this'關鍵字替換'getContext()'。 – Amy

+0

@Amy替換getContext()後方法甚至沒有運行:( –

+0

你是什麼類的超類? – Amy

回答

0

從與PowerProfile類的源代碼一起附着在文檔中,方法getAveragePower() 回報:

平均電流,單位爲毫安。

相反,你必須使用getBatteryCapacity()返回

以mAh

電池容量,因此,你必須改變方法調用的getBatteryCapacity()不帶任何參數不像getAveragePower()這需要兩個參數,所以代碼將如下所示:

public void getBatteryCapacity() { 
    Object mPowerProfile_ = null; 

    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) { 
     e.printStackTrace(); 
    } 

    try { 
     double batteryCapacity = (Double) Class 
       .forName(POWER_PROFILE_CLASS) 
       .getMethod("getBatteryCapacity") 
       .invoke(mPowerProfile_); 
     Toast.makeText(MainActivity.this, batteryCapacity + " mah", 
       Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+1

它也給出2000,因爲getBatteryCapcity返回getAveragePower –

+0

令人印象深刻:DI認爲谷歌欺騙我們:D –

+0

CPU-Z如何顯示準確? –

相關問題