我想用一个线程在后台花费几秒钟做一些工作。 我试图显示一个进度对话框,但它根本没有显示。如何使用异步任务
我认为使用异步任务会更好,但每当我尝试时都无法使其工作,主要是因为我无法访问需要在异步任务中修改的变量。
这里是我的线程在单击按钮时被调用:
Thread thread = new Thread()
{
@Override
public void run() {
try {
for (String s : dLinks) {
String pathToFile = s.substring(1));
dURLs.add(dFs.fetchLink(pathToFile, false).toString());
}
} catch (DbxException e) {
dURL = null;
e.printStackTrace();
}
}
};
thread.start();
try {
thread.join(); // I added this as the next part of my code was executing before the thread was finished
} catch (InterruptedException e) {
e.printStackTrace();
}
我能移动这一个异步任务,更新进度条,或者至少表现出旋涂器传回的阵列dURLs到主线程?
从另一个线程访问UI元素,使用'runOnUiThread()'或'Handler' –
也许这可以帮助.... http://www.vogella.com /tutorials/AndroidBackgroundProcessing/article.html – SMR
我不确定我是否能够使用异步任务,是否有使用上述代码并显示进度条,只是为了让用户知道发生了什么。 .. – user3437721