之前做的程序判断车机启动后是否插有USB设备时,直接通过USB路径里面的文件个数listFiles方法判断的,感觉不是太合理。换种思路,根据车机系统中的 /proc/mounts文件内容去判断。代码如下:
public static boolean isUdiskExist()
{
Log.d("StorageDeviceManager", "isUdiskExist");
String path = "/proc/mounts";
boolean ret = false;
try {
String encoding = "GBK";
File file = new File(path);
if ((file.isFile()) && (file.exists()))
{
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while (((lineTxt = bufferedReader.readLine()) != null) && (!ret)) {
String[] a = lineTxt.split(" ");//将读出来的一行字符串用 空格 来分割成字符串数组并存储进数组a里面
String str = a[0];//取出位置0处的字符串
if ((str.contains("/dev/block/vold")) &&
(a[1].contains("udisk"))) {
ret = true;
}
}
read.close();
} else {
Log.d("StorageDeviceManager", "can't find file: " + path);
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}