Android开发:如何用TextView显示电池充电/USB/AC充电的True/False状态
Hey there! Let's get those three charging status TextViews showing True/False correctly. You've already nailed the battery percentage, so we just need to extend that logic to capture the charging state flags and update your UI. Here's a step-by-step solution tailored to your code:
Key Background
The system sends a sticky broadcast (Intent.ACTION_BATTERY_CHANGED) with all battery-related info, including charging type. We'll use BatteryManager.EXTRA_PLUGGED to check which charging source is active:
BATTERY_PLUGGED_NONE(0): Not chargingBATTERY_PLUGGED_AC(1): Charging via AC adapterBATTERY_PLUGGED_USB(2): Charging via USB
Note: This broadcast can't be received via static manifest registration—you need to register it dynamically in your Activity.
Solution Code
1. Update your MainActivity
Replace your existing Activity code with this (it includes dynamic receiver registration and UI updates):
public class MainActivity extends AppCompatActivity { private TextView tvBatteryPercent; private TextView tvBatteryCharging; private TextView tvUsbCharging; private TextView tvAcCharging; private BroadcastReceiver batteryReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Link TextViews from your layout tvBatteryPercent = findViewById(R.id.tv_battery_percent); tvBatteryCharging = findViewById(R.id.tv_battery_charging); tvUsbCharging = findViewById(R.id.tv_usb_charging); tvAcCharging = findViewById(R.id.tv_ac_charging); // Create receiver to handle battery updates batteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Update battery percentage (your existing logic) int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPercent = level / (float) scale * 100; tvBatteryPercent.setText(String.format("%.0f%%", batteryPercent)); // Get charging source info int pluggedStatus = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); // 1. General battery charging status (True if any charging is active) boolean isCharging = pluggedStatus != BatteryManager.BATTERY_PLUGGED_NONE; tvBatteryCharging.setText(String.valueOf(isCharging)); // 2. USB charging status boolean isUsbCharging = pluggedStatus == BatteryManager.BATTERY_PLUGGED_USB; tvUsbCharging.setText(String.valueOf(isUsbCharging)); // 3. AC charging status boolean isAcCharging = pluggedStatus == BatteryManager.BATTERY_PLUGGED_AC; tvAcCharging.setText(String.valueOf(isAcCharging)); } }; // Register the receiver to listen for battery changes IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); // Unregister receiver to avoid memory leaks if (batteryReceiver != null) { unregisterReceiver(batteryReceiver); } } }
2. Update your Layout (if needed)
Make sure your XML layout has the four TextViews with matching IDs:
<!-- Example layout snippet --> <TextView android:id="@+id/tv_battery_charging" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="False" android:textSize="18sp"/> <TextView android:id="@+id/tv_usb_charging" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="False" android:textSize="18sp"/> <TextView android:id="@+id/tv_ac_charging" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="False" android:textSize="18sp"/> <TextView android:id="@+id/tv_battery_percent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0%" android:textSize="18sp"/>
3. Remove Static Receiver from Manifest
Since we're using dynamic registration, you can delete any static <receiver> entry for your battery receiver in AndroidManifest.xml—it won't work for this broadcast anyway.
How It Works
- The dynamic receiver listens for every battery state change (including when the app starts, thanks to the sticky broadcast).
- We check
pluggedStatusto determine which charging type is active, then set each TextView to "True" or "False" accordingly. - We unregister the receiver in
onDestroy()to prevent memory leaks.
内容的提问来源于stack exchange,提问作者user3215990

