You can use the following receiver, android.intent.action.UMS_CONNECTED
<receiver android:name="org.usbcheck.USBReceiver" >
<intent-filter>
<action android:name="android.intent.action.UMS_CONNECTED" />
<action android:name="android.intent.action.UMS_DISCONNECTED" />
</intent-filter>
</receiver>
For Logging
public class USBReceiver extends BroadcastReceiver {
public Context _context;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.UMS_CONNECTED")) {
Log.d("Connected", "Log Message");
}
else if (intent.getAction().equalsIgnoreCase(
"android.intent.action.UMS_DISCONNECTED")) {
Log.d("DisConnected", "Log Message");
}
}
}
The main problem while checking android.intent.action.ums_connected is
that devices using the MTP protocol (such as the Samsung Nexus Galaxy)
don't receive this broadcast.
source : Android: Detecting USB
alternatively check for charging in case device has usb charging mode
// How are we charging?
int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
source: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html