Monday, June 11, 2012

Android: Get Power status (if device is plugged in)

(From Activity)

public boolean getPowerStatus() {
        Intent intent = this.registerReceiver(null,
                    new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int isConnected = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return isConnected == BatteryManager.BATTERY_PLUGGED_AC ||
                    isConnected == BatteryManager.BATTERY_PLUGGED_USB;
    }

No comments:

Post a Comment