El estado de la batería no siempre se está cargando.
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
if (status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL)
Toast.makeText(context, "Charging!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "Not Charging!", Toast.LENGTH_SHORT).show();
}
Manifiesto:
<receiver android:name=".receiver.BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
En este código, el Toast siempre muestra "¡No está cargando!". Probé esto en un dispositivo real, y cuando lo conecto a la alimentación de CA o USB, todavía muestra el mensaje "¡No se está cargando!" Tostada.