デバイスが現在充電中かどうかを検出しようとしていますが、このコードは常にisCharging
変数をfalse
次のように設定します。
public class PowerReceiver 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;
if (isCharging)
{
Toast.makeText(context, "Charging", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Not charging", Toast.LENGTH_LONG).show();
}
}
}