Android 如何判断手机处于电源充电状态仍是USB链接状态?

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatusIntent = registerReceiver(null, ifilter);
//若是设备正在充电,能够提取当前的充电状态和充电方式(不管是经过 USB 仍是交流充电器),以下所示:

// Are we charging / charged?
int status = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
        status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging?
int chargePlug = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

if (isCharging) {
    if (usbCharge) {
        Toast.makeText(MainActivity.this, "手机正处于USB链接!", Toast.LENGTH_SHORT).show();
    } else if (acCharge) {
        Toast.makeText(MainActivity.this, "手机经过电源充电中!", Toast.LENGTH_SHORT).show();
    }
} else {
    Toast.makeText(MainActivity.this, "手机未链接USB线!", Toast.LENGTH_SHORT).show();
}

来源:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1231/761.htmlhtml