1,USB存儲設備(如:U盤,移動硬盤): html
//USB存儲設備 插拔監聽與 SD卡插拔監聽一致。 android
private USBBroadCastReceiver mBroadcastReceiver; shell
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); ide
private class USBBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_EJECT)) { ui
//USB設備移除,更新UI
} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { spa
//USB設備掛載,更新UI
}
}
} orm
//得到掛載的USB設備的存儲空間使用狀況 htm
public static String getUSBStorage(Context context){
// USB Storage ci
//storage/udisk爲USB設備在Android設備上的掛載路徑.不一樣廠商的Android設備路徑不一樣。 get
//這樣寫一樣適合於SD卡掛載。
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;//空間:已使用/可用的
}
2,USB外接輸入設備(如:鍵盤,鼠標,掃描槍)
try {
//得到外接USB輸入設備的信息
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();
//對獲取的每行的設備信息進行過濾,得到本身想要的。
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
另:設備信息也能夠經過 adb shell 進入執行 cat /proc/bus/input/devices看到。
USB外接輸入設備信息打印以下:
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 這行信息會在Android設備與USB設備數據交互的是否使用到。
關於USB外接設備如何與Android設備數據數據交互的代碼書寫能夠參考:
http://developer.android.com/guide/topics/connectivity/usb/index.html
N: Name="ACRUX USB Keyboard" 這行說明了外接的USB設備的名稱。
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
這兩行可用於識別該USB設備鏈接在Android設備的哪個USB口.