17

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
9

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.

  • 1
    But what exactly happens when I tap on the "Turn on USB storage" button? Why isn't it possible to achieve the effect of this button press even when you go to the extreme of writing a whole new app? Wouldn't a more intelligent variant of the "monkey" utility do the trick? – Anonymous Howard Jul 15 '13 at 4:04
  • I've explained that in my answer: it calls the functions I mentioned, which in turn call into a system service, and the system service does all the work. For more detail you'll have to read the source. You can't even use monkey to click the button, as the code has an explicit check for that, to stop tests accidentally unmounting storage. – Dan Hulme Jul 15 '13 at 7:38
  • You could try the service CLI tool which can call system service methods. – Léo Lam Aug 21 '15 at 11:41
5


I tried the "StorageManager.enableUsbMassStorage" and the "StorageManager.disableUsbMassStorage" to enable and disable the mass storage and its works.
Thanks.

That methods are private method. So, If you want to use them, You shoud do it by reflection. To get that Storage Manager, you might use getSystemService(java.lang.String) method that included in the activities:

 StorageManager storage = (StorageManager)getApplicationContext().getSystemService(STORAGE_SERVICE);
 Method method = storage.getClass().getDeclaredMethod("enableUsbMassStorage");
 method.setAccessible(true); 
 Object r = method.invoke(storage);

And to disble mass storage:

StorageManager storage = (StorageManager) getApplicationContext().getSystemService(STORAGE_SERVICE);
Method method = storage.getClass().getDeclaredMethod("disbleUsbMassStorage");
method.setAccessible(true);
Object r = method.invoke(storage);
  • Amir Brand- I have tried to use this method but i am not able to disable my usb storage. Is it working with you ? if yes ,can you please help me out .thanks – Khanna Jun 24 '15 at 4:43
  • Hi Khanna Sure. No problem. Please send me mail to netmir2002@gmail.com with the problem and I will try to find the solution. – Amir Brand Jul 14 '15 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.