@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();
}
显现:
<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>
在此代码中,Toast 始终显示“未充电!”。我在实际设备上进行了测试,当我将其插入 AC 或 USB 电源时,它仍然显示“未充电!” 吐司。