doInBackground 调用 AsyncTask 然后休眠阻塞 UI 线程
我有一个活动,由 AsyncTask 组成,旨在当用户单击按钮时启动请求。我一直在寻找答案,但我没有找到同样的问题,或者它对我来说不一样。代码正在执行我想要的操作,但 ProgressDialog 看起来被阻止,因为微调器有时(几乎一直)不转动。
当我单击按钮时:
AsyncTask 启动 -> showDialog() 被调用 onPreExecute -> startSearch(SearchManager 启动一个新的 AsyncTask,在 doInBackground 中有大量网络调用)-> Activity中的doInBackground等待SearchManager加载 ->展示。
按钮代码:
button_search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new SearchTask().execute();
}
});
搜索活动中 AsyncTask 代码:
private class SearchTask extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute(){
showDialog(DIALOG_LOADING_ID);
searchManager.startSearch();
}
@Override
protected Void doInBackground(Void... params) {
while(searchManager.isLoading()){
try {Thread.sleep(150);} catch(Exception e){};
}
return null;
}
@Override
protected void onPostExecute(Void ret){
try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};
if ( searchManager.errorOccurred() ){
//Error
} else {
//No Error
}
}
SearchManagerAsyncTask 代码:由 startSearch 直接启动
protected class SearchAsync extends AsyncTask <Void,Void,Void>{
@Override
protected Void doInBackground(ComSearchAds... datas) {
global.getDataManager().doSearch();
//... When finished
setIs_loading(false);
}
}
我显然做错了什么,但找不到什么以及如何避免这种情况。感谢您的帮助 !
解决方案:
最后,看起来不旋转的 ProgressDialog 是因为我使用了相同的 ProgressDialog 实例并与
showDialog(DIALOG_LOADING_ID);
//doInBackground
dismissDialog(DIALOG_LOADING_ID);
导致问题一起使用,我更改为
removeDialog(DIALOG_LOADING_ID)
现在它工作正常。
谢谢大家,希望有一天它可以帮助别人!
I have an activity, composed of an AsyncTask aiming to launch a request when the user clicks on the button. I have been looking for answers, but I didn't find the same problem, or it didn't the same for me. The code is doing what I want, but the ProgressDialog looks blocked as the spinner is not turning sometimes (almost all the time).
When I click on the button :
AsyncTask is launched -> showDialog() is called onPreExecute -> startSearch ( SearchManager launches a new AsyncTask with in the doInBackground there is a heavy call with network ) -> doInBackground in Activity waits for SearchManager to be loaded -> display.
Code for button :
button_search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new SearchTask().execute();
}
});
Code for AsyncTask in Search Activity :
private class SearchTask extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute(){
showDialog(DIALOG_LOADING_ID);
searchManager.startSearch();
}
@Override
protected Void doInBackground(Void... params) {
while(searchManager.isLoading()){
try {Thread.sleep(150);} catch(Exception e){};
}
return null;
}
@Override
protected void onPostExecute(Void ret){
try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};
if ( searchManager.errorOccurred() ){
//Error
} else {
//No Error
}
}
Code for SearchManagerAsyncTask : which is directly launched by startSearch
protected class SearchAsync extends AsyncTask <Void,Void,Void>{
@Override
protected Void doInBackground(ComSearchAds... datas) {
global.getDataManager().doSearch();
//... When finished
setIs_loading(false);
}
}
I'm apparently doing something wrong, but can't find what and how to avoid this. Thanks for your help !
SOLUTION :
Finally, it appears that the not spinning ProgressDialog was because I was using the same instance of ProgressDialog and
showDialog(DIALOG_LOADING_ID);
//doInBackground
dismissDialog(DIALOG_LOADING_ID);
used with causes problem, I changed to
removeDialog(DIALOG_LOADING_ID)
and now it's working fine.
Thanks All, and hope it can help someone someday !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要创建另一个任务,只需通过另一个活动进行搜索即可。
您需要做的就是将搜索代码放入搜索任务的 doInbackGround() 中。例如
还使用类级别变量来获取搜索结果来存储 true/false
You don't need to create another task , just instead of doing the search stuff via another activity.
all you need to do is to put you search cod ein doInbackGround() of search task. e.g
also use class level variable to get search result to store true/false
您能否尝试在布局文件中添加一个 ProgressBar 控件,并在启动异步任务时将其 Visible 设置为 true,并在进程结束时将其设置为 gone。下面是相同的代码。
Could you please try to add a ProgressBar control in your layout file and set its Visible to true when starting async task and set to gone when end of process reached. Below is the code for the same.
很难说,但是尝试一下 onClickListner:
这是一个非常疯狂的猜测,但它可能会起作用
It's hard to say, but try this for onClickListner:
It's a pretty wild guess, but it might work