Is it possible to disconnect USB storage on Android devices?

I tried searching over and over and was confused, can we control the USB stick without rooting the phone, if so how? I tried the following way to enable and disable permission, but to no avail:

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("disableUsbMassStorage");
method.setAccessible(true);
Object r = method.invoke(storage);

      

If anyone knows about this, please share your knowledge.

Thanks in advance.

+3


source to share


2 answers


The short answer to your question is NO . You cannot programmatically play with USB mass storage resolutions. You also won't find Android apps that can do this reliably for any / all phones.

By default, the Android framework does not allow third-party application software to bypass USB storage permissions. This choice always remains for the user, and this is the right approach. Think about it: if it were allowed, any third-party application could misuse this permission and transfer data over the USB connection.



The approach you tried to use assumes that we know the names of the corresponding methods and call them through reflection. This will work for some OEMs who have changed the underlying structure and exposed API calls accordingly. But it assumes that you know the names of these methods, and it will only work on a tiny subset of the huge number of Android phone models. It won't work on most phones, and it certainly won't work on any Nexus Android OS source device.

That's all.

+3


source


If your SD card is currently SHARED, it means it is connected to PC in MSC mode, you can check this case as follows:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_SHARED.equals(state)) {
// Sd card has connected to PC in MSC mode
}

      



You can now force disable usb bulk storage by calling:

MountService.setUsbMassStorageEnabled(false);



//or



StorageManager.disableUsbMassStorage();



//but unfortunately, both these API are not public accessible?!

      

+1


source







All Articles