Android 充電資訊的獲取【轉】
本文轉載自:https://blog.csdn.net/wateryi/article/details/50834821
在android系統中,電池資訊是由BatteryService.java統一管理的,在BatteryService中,通過:
IBinder b = ServiceManager.getService("batteryproperties");
final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
IBatteryPropertiesRegistrar.Stub.asInterface(b);
try {
batteryPropertiesRegistrar.registerListener(new BatteryListener());
} catch (RemoteException e) {
// Should never happen.
}
向註冊回撥函式,電池資訊一旦有更新,就會呼叫BatteryListener中的batteryPropertiesChanged介面:
private final class BatteryListener extends IBatteryPropertiesListener.Stub {
@Override
public void batteryPropertiesChanged(BatteryProperties props) {
final long identity = Binder.clearCallingIdentity();
try {
BatteryService.this.update(props);
} finally {
Binder.restoreCallingIdentity(identity);
}
}
}
進而呼叫update函式更新電池資訊類:BatteryProperties; 而通常上層接收的電池資訊廣播也是通過BatteryService中的sendIntentLocked傳送的:
private void sendIntentLocked() {
// Pack up the values and broadcast them to everyone
final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
| Intent.FLAG_RECEIVER_REPLACE_PENDING);
int icon = getIconLocked(mBatteryProps.batteryLevel);
intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
intent.putExtra(BatteryManager.EXTRA_STATUS_SMARTBOOK, mBatteryProps.batteryStatus_smb);
intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
intent.putExtra(BatteryManager.EXTRA_PRESENT_SMARTBOOK, mBatteryProps.batteryPresent_smb);
intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
intent.putExtra(BatteryManager.EXTRA_LEVEL_SMARTBOOK, mBatteryProps.batteryLevel_smb);
intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
mHandler.post(new Runnable() {
@Override
public void run() {
ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
}
});
}
所以我們上層得到電池資訊可以通過廣播獲取:
private BroadcastReceiver mChargeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int status = intent.getIntExtra("status", 0);
int chargeLevel = intent.getIntExtra("level", 0);
Log.i("wrx_charge", "status = " + status);
Log.i("wrx_charge", "chargeLevel = " + chargeLevel);
String chargeStatus = "";
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_unknow);
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_charging);
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_discharging);
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_notcharging);
break;
case BatteryManager.BATTERY_STATUS_FULL:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_chargefull);
break;
}
mChargeString = chargeLevel + "% " + chargeStatus;
if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) {
isCharging = true;
} else {
isCharging = false;
}
onChargeChanged();
}
}
};
private void registerChargeReceiver() {
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
getContext().registerReceiver(mChargeReceiver, filter);
}
private void unregisterChargeReceiver() {
getContext().unregisterReceiver(mChargeReceiver);
}
其實手機內部的linux核心的目錄下也是有相關電池資訊節點的;具體目錄如下:
#define AC_ONLINE_PATH "/sys/class/power_supply/ac/online"
#define USB_ONLINE_PATH "/sys/class/power_supply/usb/online"
#define BATTERY_STATUS_PATH "/sys/class/power_supply/battery/status"
#define BATTERY_HEALTH_PATH "/sys/class/power_supply/battery/health"
#define BATTERY_PRESENT_PATH "/sys/class/power_supply/battery/present"
#define BATTERY_CAPACITY_PATH "/sys/class/power_supply/battery/capacity"
#define BATTERY_VOLTAGE_PATH "/sys/class/power_supply/battery/batt_vol"
#define BATTERY_TEMPERATURE_PATH "/sys/class/power_supply/battery/batt_temp"
#define BATTERY_TECHNOLOGY_PATH "/sys/class/power_supply/battery/technology"
其中capacity的值就是電池電量的值;我們可以通過以下方式去讀取手機相關節點下的值(試過在eng手機中是可以獲取到的,不知道user手機會不會存在相關許可權問題?):
try {
FileReader localFileReader = new FileReader("檔案路徑");
BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
str2 = localBufferedReader.readLine();//獲取到的檔案中的值
localBufferedReader.close();
} catch (IOException e) {
}