1,USB storage devices (such as: USB flash drive, mobile hard disk):
USB storage Device Plug-and-draw monitoring is consistent with SD card Plug and draw monitoring.
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)) {
USB device removal, updating UI
} else if (Action.equals (intent.action_media_mounted)) {
USB device mount, update UI
}
}
}
Get storage space usage for mounted USB devices
public static String Getusbstorage (context context) {
USB Storage
Storage/udisk is the mount path of the USB device on the Android device. Different manufacturers ' Android device paths are different.
This write 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 (e.g. keyboard, mouse, scanning gun)
try {
Get information about an external USB input device
Process p=runtime.getruntime (). EXEC ("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 for each line you get and get what you want.
}
} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}
Another: Device information can also be seen through the ADB shell into the execution cat/proc/bus/input/devices.
The 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 to interact with the USB device data on the Android device.
Code writing about how a USB external device interacts with the Android device data data can be consulted:
Http://developer.android.com/guide/topics/connectivity/usb/index.html
N:name= "Acrux USB Keyboard" This line describes 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 the USB device is connected to on the Android device.
Android Monitor detects USB storage devices