执行异步任务时按钮保持按下状态 [英] button remains pressed while the Asynctask is being executed
问题描述
我有一个按钮,按下后会执行以下代码:
I have a button, which on pressed, executes the following code:
public void onClick(View v) {
// TODO Auto-generated method stub
//progressSpin.setVisibility(View.VISIBLE);
try {
data=new WebkioskExtractor().execute(username,password).get();
System.out.println("Data = "+data);
} catch (Exception e) {
// TODO Auto-geneorated catch block
e.printStackTrace();
}
//progressSpin.setVisibility(View.GONE);
}
从代码中可以清楚地看出,我必须等待 AsyncTask 完成,因为我依赖于它返回的数据.问题是当任务正在执行时(它从互联网上获取一些数据)按钮保持按下状态.即使我将创建的进度条设置为可见,它也不会显示.
as clear from the code, i have to wait for the AsyncTask to finish because i am relying on the data it returns. the problem is that while the task in being executed (it fetches some data from the internet) the button remains in the pressed state. even if i set the progressbar i created to VISIBLE, it does not show up.
我该如何解决这个问题?我希望按下按钮一次,然后进度条应该开始旋转,但事实并非如此.
How can i fix this? I would like the button to be pressed once and then the progressbar should start spinning, which isn't happening.
推荐答案
不要使用 get()
.
data=new WebkioskExtractor().execute(username,password).get(); // Bad! :(
data=new WebkioskExtractor().execute(username,password); // Good! :)
它阻止了 UI
这就是为什么你的 Button
保持按下状态.这也是您的 ProgressBar
没有显示的原因(它也在 UI
上运行).我假设你在 onPreExecute()
和 dismiss()
中开始你的 ProgressBar
它在你的 onPostExecute()
代码>异步任务代码>.如果没有,这就是你应该做的.如果您的 AsyncTask
中的所有其他内容都设置正确,那么删除 .get()
应该可以解决您的问题.
It blocks the UI
which is why your Button
remains pressed. This is also why your ProgressBar
isn't showing up (it also runs on the UI
). I assume you start your ProgressBar
in onPreExecute()
and dismiss()
it in onPostExecute()
of your AsyncTask
. If not, this is what you should be doing. If everything else in your AsyncTask
is set up correctly then removing .get()
should fix your problem.
将您的结果从 doInBackground()
返回到 onPostExecute()
,这应该会提供您想要的结果.除了 doInBackground() 之外,您还可以在
.onPostExecute()
或 AsyncTask
的任何其他方法的 UI
上执行任何您需要的操作
Return your result from doInBackground()
to onPostExecute()
and this should give you what you want. You can also do whatever you need to on the UI
from onPostExecute()
or any other method of the AsyncTask
besides doInBackground()
.
进度条
您不需要在 ProgressBar
上设置 Visibility
.看这个例子:
You don't need to set the Visibility
on the ProgressBar
. See this example:
public class GetUsersTask extends AsyncTask<Void, Void, Void> {
ProgressDialog progress = ProgressDialog.show(LoginScreen.this, "Downloading Users", "Please wait while users are downloaded");
// you can create it here
@Override
protected void onPreExecute()
{
// show it here like so
progress.setCancelable(false);
progress.isIndeterminate();
progress.show();
}
@Override
protected void onPostExecute(Void result) {
// and dismiss it here
progress.dismiss();
}
}
@Override
protected void onProgressUpdate(Void... values) {
// can update the ProgressBar here if you need to
}
@Override
protected Void doInBackground(Void... params) {
...
}
使用回调
如果您需要从 AsyncTask
获取不是活动的内部类的结果,那么您可以使用 interface
与一个 callBack
.更多关于在这个答案中这样做
Using a callback
If you need to get a result from AsyncTask
which is not an inner-class of your Activity then you can use an interface
with a callBack
. More about doing that in this answer
这篇关于执行异步任务时按钮保持按下状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!