2013-01-18 61 views
0

我想在不使用java的情況下讀取本地應用程序中的當前電池電量。 Java中的等價物是:native app get batterylevel

BroadcastReceiver batteryReceiver = new BroadcastReceiver() { 
    int scale = -1; 
    int level = -1; 
    int voltage = -1; 
    int temp = -1; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
     scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 
     temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); 
     voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); 
     Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage); 
    } 
}; 

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 
registerReceiver(batteryReceiver, filter); 

這怎麼可能只用本機代碼?

回答

1

查看有關dumpsys實用程序的主題可能以編程方式運行並讀取IO輸出緩衝區。我發現你需要尋找的幾個C++源代碼。運行dumpsys可能需要root權限。

What's the Android ADB shell "dumpsys" tool and what are its benefits?

http://source.android.com/tech/input/dumpsys.html

http://code.metager.de/source/xref/android/4.1.1/frameworks/base/services/java/com/android/server/BatteryService.java

http://code.metager.de/source/xref/android/4.1.1/frameworks/native/cmds/dumpsys/dumpsys.cpp

http://code.metager.de/source/xref/android/4.1.1/frameworks/base/services/jni/com_android_server_BatteryService.cpp

http://code.metager.de/source/xref/android/4.1.1/frameworks/base/services/jni/onload.cpp

如果我正確的話,C++源代碼讀取/ sys/class/power_supply虛擬目錄,但是會發生什麼,有人可能會告訴你更多關於Linux系統的內部細節。大多數代碼似乎是JNI接口的東西,更新BatteryService.java類中的私有成員字段。 OnLoad類註冊jni < - > Java鉤子,但那不是你需要知道的,僅供參考。

+0

我已經想過用dumpsys來讀取關卡的內容,但正如您已經說過的那樣,它需要root權限......但最終它爲我工作,所以謝謝! – bricklore

相關問題