我有这个过程通过一些“如果”运行,具体取决于用户是否插入或弹出内存笔式驱动器:
int userStatus = StopActivity.USER_HAS_NEVER_INSERTED_MEMORY;
while (true) {
File usbDirectoryFile = new File(usbDirectory);
if (usbDirectoryFile.exists()) {
verMasInfoDirectory = usbDirectory + File.separator + "vermasinfo";
File verMasInfoDirectoryFile = new File(verMasInfoDirectory);
if (verMasInfoDirectoryFile.exists()) {
break;
} else if (userStatus != StopActivity.USER_HAS_BEEN_NOTIFIED) {
userStatus = StopActivity.USER_HAS_BEEN_NOTIFIED;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(StopActivity.this, R.string.stop_activity_directory_not_exists_toast_text, Toast.LENGTH_LONG).show();
}
});
}
} else if (userStatus == StopActivity.USER_HAS_BEEN_NOTIFIED) {
userStatus = StopActivity.USER_HAS_NOT_BEEN_NOTIFIED;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(StopActivity.this, R.string.stop_activity_ejected_memory_toast_text, Toast.LENGTH_LONG).show();
}
});
}
usbDirectoryFile = null;
}
问题是当用户快速插入/弹出笔式驱动器时,对 runOnUIThread 的调用都可能导致死锁。
我怎样才能避免这种僵局?
提前致谢。