如何在暂停主执行但不暂停 UI 的同时解析 xml 文件
我需要解析一个xml文件,大约需要3秒,并在解析完成后立即使用从xml文件解析的数据。然而,由于大约需要 3 秒才能完成,所以我不希望屏幕只是等待 3 秒而不显示任何内容。我希望它显示一个 ProgressDialog。 执行所有 3 个。
基本上,我有一个按钮,按下该按钮时,我想解析 xml 文件,然后使用从文件中解析的数据:
void onButtonClicked() {
ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Parsing...");
pd.show();
String[] ret;
return = parseXmlFile();
pd.dismiss();
if (ret[0] == "steve") {
sayHiToSteve();
}
}
我已经尝试了这些线程中的所有内容,虽然大多数都执行 2 / 3,但似乎没有一个 我使用 asyncTask,它将继续执行并在任务完成之前检查 ret[0],并且将为 null。 如果我执行 AsyncTask.get() ,它会暂停执行但不显示进度对话框,所以这也不好。
我也考虑过创建一个单独的线程,但我遇到了同样的问题。我无法使用计时器,因为它大约需要 3 秒(但如果连接速度慢,可能长达 4 或 5 秒),所以我无法估算。
非常感谢任何帮助。 提前致谢。
I need to parse an xml file which takes about 3 seconds, and immediately use the data that is parsed from the xml file after it has completed. However, since it takes around 3 seconds to complete, i don't just want the screen sitting and waiting for 3 seconds showing nothing. I would like it to display a ProgressDialog. I've tried everything in these threads and while most do 2 / 3, none seem to do all 3.
Basically I have a button that when pressed, i want to parse the xml file and then use the data parsed from the file:
void onButtonClicked() {
ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Parsing...");
pd.show();
String[] ret;
return = parseXmlFile();
pd.dismiss();
if (ret[0] == "steve") {
sayHiToSteve();
}
}
If I use an asyncTask, it will continue on with execution and check ret[0] before the task is completed and will just be null.
if i do AsyncTask.get(), it pauses the execution but doesn't display the progress dialog, so that's no good either.
I also looked at creating a seperate thread, but i had the same problem. I can't use a timer, because it is about 3 seconds (but can be up to 4 or 5 seconds if connection is slow), so i can't approximate.
Any help is much appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为此使用 AsyncTask,例如:
然后要使用它,您可以执行以下操作:
You can use an AsyncTask for this, something like:
then to use it you do:
在主线程中加入怎么样?
请参阅线程连接在 Android 中不起作用?
和 http://docs.oracle.com/javase/tutorial/essential /concurrency/join.html
How about a join in the main thread.
See thread join not working in Android?
and http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html