你可以使用BatteryManager電池電量。看到該文檔here
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level/(float)scale;
現在從取決於batterPct的代碼中設置特定的圖像
,並監測變化使用以下
聲明一個BroadcastReceiver
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
在關聯的BroadcastReceiver實施,您可以按照上一步中的描述提取當前充電狀態和方法。
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}
是的,但如何插入圖像,並在batteryPct達到該級別時更改它? (100%,50%和20%)?所有由java我認爲。 –
查看已更新的答案 – stinepike
謝謝,並插入圖像? –