【发布时间】:2014-10-28 09:22:36
【问题描述】:
我正在使用线程来定期检查 pendrive 是否连接,在 EMPTY / READY 两种状态之间切换
@Override
public void run() {
running = true;
while(running){ // forever
try {
Delay.of_1second();
drivePath = getDrivePath(rootPath);
if (isReady() && drivePath==null){
notifyNewState(DriveState.EMPTY);
}
else if (!isReady() && drivePath!=null){
notifyNewState(DriveState.READY);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private String getDrivePath(String rootPath){
File file = new File(rootPath);
if (SystemUtils.IS_OS_WINDOWS){
return file.exists() ? rootPath : null;
}
else{
for (File f : file.listFiles()){
if (!f.getName().equalsIgnoreCase("CDROM")){
return f.getAbsolutePath(); // we return the first folder other than 'CDROM'
}
}
}
return null;
}
它在具有 rootPath="E:\" 的 Windows 中工作得很好,但在 Ubuntu rootPath="/media/ubuntu" 中不起作用,我仍然不明白的奇怪点是 线程工作如果我在文件管理器中打开 pendrive 很好,但我需要它在 pendrive 插入 USB 端口后立即工作,而不是当用户使用文件管理器浏览文件时。我能做什么?
【问题讨论】: