Toggling USB mass storage on (or off) via adb commands Background: I have a USB stick Android device (e.g. RikoMagic) which I mostly run in "headless" mode. Among other things, I've already figured out how to shut it down via a combination of adb and the internal "am" command:

adb shell am start -n android/com.android.server.ShutdownActivity


I'm now trying to figure out how to turn USB mass storage support on and off. I've got as far as having the USB storage activity window pop up via the following command:

adb shell am start -n com.android.systemui/.usb.UsbStorageActivity


Unfortunately I haven't been able to figure out how to send the equivalent "intent" to actually "unmount" or "unshare" the shared volume (or "mount" or "share" it for that matter). The following command does nothing:

adb shell am start -n com.android.systemui/.usb.UsbStorageActivity -a android.intent.action.MEDIA_UNSHARED file:///mnt/sdcard


What am I doing wrong? What's the magic incantation to mount/unmount the internal storage of an Android device?

Note 1: I know there's the obvious Linux "mount"/"umount" command. However, simply mounting or unmounting the volume won't trigger system functions vital to, for example, restarting or stopping apps that have been moved to the SD card. A "umount" will probably fail anyway when an app in the SD card is still active.

Note 2: Apparently there's a "setprop" command that can enable/disable USB mass storage support UNTIL the next reboot. Again, this all or nothing approach isn't what I'm looking for but the ability to toggle USB mass storage on and off during a single session, as can be done by the touch interface.

adb shell setprop sys.usb.config mass_storage
Alas, the UsbStorageActivity doesn't use an intent to enable and disable USB mass storage, so there's no way to achieve this using am. It instead calls the functions StorageManager.enableUsbMassStorage and `StorageManager.disableUsbMassStorage to do its work. As these functions are hidden in the framework, it's not possible to write an app to do this, either. As far as I know, there's no existing command to do this.

It might be possible to write an app that offers a command-line interface and uses the bound system service MountService to call its setUsbMassStorageEnabled function, but that would be way off-topic for this site.