1. USB storage devices (such as USB flash drives and mobile hard drives ):
// The USB storage device plugging listening is consistent with the SD card plugging listening.
Private USBBroadCastReceiver mBroadcastReceiver;
IntentFilter iFilter = new IntentFilter ();
IFilter. addAction (Intent. ACTION_MEDIA_EJECT );
IFilter. addAction (Intent. ACTION_MEDIA_MOUNTED );
IFilter. addAction (Intent. ACTION_MEDIA_REMOVED );
IFilter. addAction (Intent. ACTION_MEDIA_UNMOUNTED );
IFilter. addDataScheme ("file ");
MBroadcastReceiver = new USBBroadCastReceiver ();
RegisterReceiver (mBroadcastReceiver, iFilter );
Private class USBBroadCastReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
String action = intent. getAction ();
If (action. equals (Intent. ACTION_MEDIA_EJECT )){
// Remove the USB device and update the UI
} Else if (action. equals (Intent. ACTION_MEDIA_MOUNTED )){
// Mount the USB device to update the UI
}
}
}
// Obtain the storage space usage of the mounted USB device
Public static String getUSBStorage (Context context ){
// USB Storage
// Storage/udisk is the Mount path of the USB device on the Android device. The paths of Android devices of different manufacturers are different.
// This write method is also suitable for SD card mounting.
File path = new File ("/storage/udisk ");
StatFs stat = new StatFs (path. getPath ());
Long blockSize = stat. getBlockSize ();
Long totalBlocks = stat. getBlockCount ();
Long availableBlocks = stat. getAvailableBlocks ();
String usedSize = Formatter. formatFileSize (context, (totalBlocks-availableBlocks) * blockSize );
String availableSize = Formatter. formatFileSize (context, availableBlocks * blockSize );
Return usedSize + "/" + availableSize; // space: used/available
}
2. USB external input device (for example, keyboard, mouse, scanner)
Try {
// Obtain the information of the external USB Input Device
Process p=runtime.getruntime(cmd.exe c ("cat/proc/bus/input/devices ");
BufferedReader in = new BufferedReader (new InputStreamReader (p. getInputStream ()));
String line = null;
While (line = in. readLine ())! = Null ){
String deviceInfo = line. trim ();
// Filter the device information of each row to obtain the desired information.
}
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
In addition, you can run cat/proc/bus/input/devices to view the device information through the adb shell.
USB external input device information is printed as follows:
I: Bus = 0003 Vendor = 11c0 Product = 0030 Version = 0110
N: Name = "acrux usb Keyboard"
P: Phys = usb-0000: 00: 04.0-1.3/input1
S: Sysfs =/devices/pci0000: 00/0000: 00: 04.0/usb1/1-1/1-1.3/1-1.3: 1.1/input/input3
U: Uniq =
H: Handlers = mouse1 event3
B: PROP = 0
B: EV = 17
B: KEY = 70000 0 0 0 0 0 0 0 0
B: REL = 103
B: MSC = 10
I: Bus = 0003 Vendor = 11c0 Product = 0030 Version = 0110 this line of information will be used for data interaction between Android devices and USB devices.
For code writing on how USB external devices interact with Android device data, refer:
Http://developer.android.com/guide/topics/connectivity/usb/index.html
N: Name = "acrux usb Keyboard" specifies the Name of the external USB device.
P: Phys = usb-0000: 00: 04.0-1.3/input1
S: Sysfs =/devices/pci0000: 00/0000: 00: 04.0/usb1/1-1/1-1.3/1-1.3: 1.1/input/input3
These two lines can be used to identify which USB port is connected to the Android device.