简体   繁体   English

以编程方式关闭Android设备上的USB存储

[英]Programmatically Turn Off USB Storage on Android Devices

On many android devices, when the device is plugged into the USB port of a computer or even on some USB charging devices, the phone goes into USB Storage mode. 在许多Android设备上,当设备插入计算机的USB端口或甚至某些USB充电设备时,手机将进入USB存储模式。 When the device is in this mode, android apps cannot access the sdcard. 当设备处于此模式时,Android应用程序无法访问SD卡。 Is there any way (1) to detect when the device is in this mode and (2) to programmatically turn off USB storage, at least temporarily, so my android app can access the sdcard? 有没有办法(1)检测设备何时处于此模式,(2)以编程方式关闭USB存储,至少暂时,所以我的Android应用程序可以访问SD卡?

I've seen other SO questions and the answers are not really sufficient (eg, Android: Detecting USB ). 我已经看到其他SO问题并且答案还不够(例如, Android:检测USB )。

A1: If current state of Sd card is SHARED it means that it has connected to PC in MSC mode, you can check this case as following: A1:如果Sd卡的当前状态为SHARED,则表示它已在MSC模式下连接到PC,您可以查看以下情况:

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

A2: You can force to turn off usb mass storage connection by calling: A2:您可以通过调用以下命令强制关闭USB大容量存储连接:

MountService.setUsbMassStorageEnabled(false);

or 要么

StorageManager.disableUsbMassStorage();

but unfortunately, both these API are not public accessible?! 但不幸的是,这两个API都不公开访问?!

You can detect it (you have the link), but, AFAIK, you can't mount/unmount USB storage at least not with the public SDK APIs. 您可以检测到它(您有链接),但是,AFAIK,您无法安装/卸载USB存储,至少不能使用公共SDK API。 In Honeycomb (3.0 and above), USB mass storage is no longer used to access the device's external storage, so your app and a computer can access it simultaneously. 在Honeycomb(3.0及以上版本)中,USB大容量存储不再用于访问设备的外部存储,因此您的应用程序和计算机可以同时访问它。

It blocks mass storage by using reflection on storageManger api . 它通过在storageManger api上使用反射来阻止大容量存储。 Storage Manager api is avaliable from sdk level -9. Storage Manager api可从sdk级别-9获得。

I am not able to reflect Mountservice api which can target less os level. 我无法反映可以降低操作系统级别的Mountservice api。 If any one has done it please give suggestions 如果有人这样做,请提出建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM