Use UsbManager.ACTION_USB_DEVICE_ATTACHED
and UsbManager.ACTION_USB_DEVICE_DETACHED
for your intent filter.
To check for connection to PC use Intent.ACTION_BATTERY_CHANGED
and onReceive
intent.getInt(BatteryManager.EXTRA_PLUGGED)
and see if the value is BatteryManager.BATTERY_PLUGGED_USB
.