1,USB存储设备(如:U盘,移动硬盘):
//USB存储设备 插拔监听与 SD卡插拔监听一致。
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设备移除,更新UI
} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
//USB设备挂载,更新UI
}
}
}
//获得挂载的USB设备的存储空间使用情况
public static String getUSBStorage(Context context){
// USB Storage
//storage/udisk为USB设备在Android设备上的挂载路径.不同厂商的Android设备路径不同。
//这样写同样适合于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;//空间:已使用/可用的
}
//获得外接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();
}
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口.
新闻热点
疑难解答
图片精选