3

I was wondering if would be possible to perform an action for each time the USB status of the device changes.

For example if the user plugs in the cable and it is in sync/charge mode it performs a certain action, if the user then changes the phone into USB Memory Card mode another action is performed and if the user then disconnects the cable then the another action is done.

Is this actually possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
Boardy
  • 35,417
  • 104
  • 256
  • 447

1 Answers1

5

Is this actually possible.

Sure.

Android broadcasts various Intents that you can listen to with a BroadcastReceiver. Among these are:

  • ACTION_BATTERY_CHANGED (tells you if device is plugged into USB or AC)
  • ACTION_UMS_CONNECTED and ACTION_UMS_DISCONNECTED (tells you if device enters or leaves USB Mass Storage mode)
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Be careful, `ACTION_UMS_CONNECTED` and `ACTION_UMS_DISCONNECTED` don't work if the device is using MTP protocole instead of the UMS one (for example the Galaxy Nexus is using only the MTP protocol)! – darkheir Jul 20 '12 at 07:27
  • Its deprecated in API 14 - http://developer.android.com/reference/android/content/Intent.html#ACTION_UMS_CONNECTED – Luser_k May 02 '15 at 21:33