doInBackground调用AsyncTask然后休眠阻止UI线程

时间:2021-04-24 20:57:09

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).

我有一个活动,由AsyncTask组成,旨在当用户点击按钮时发起请求。我一直在寻找答案,但我没有找到同样的问题,或者对我来说不一样。代码正在做我想要的,但ProgressDialog看起来被阻止,因为微调器有时不转(几乎所有时间)。

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.

启动AsyncTask - > showDialog()调用onPreExecute - > startSearch(SearchManager启动一个新的AsyncTask,在doInBackground中有一个大量调用网络) - > doInBackground在Activity中等待SearchManager加载 - >显示。

Code for button :

按钮代码:

button_search.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                new SearchTask().execute();

            }
        });

Code for AsyncTask in Search Activity :

搜索活动中的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

            }   
        }

Code for SearchManagerAsyncTask : which is directly launched by startSearch

SearchManagerAsyncTask的代码:由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

最后,似乎没有旋转的ProgressDialog是因为我使用的是ProgressDialog和的相同实例

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 !

谢谢大家,希望有一天能帮助别人!

3 个解决方案

#1


0  

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

你需要做的就是把搜索任务的搜索cod ein doInbackGround()。例如

@Override
protected Void doInBackground(Void... params) {

    global.getDataManager().doSearch();
    return null;
}

also use class level variable to get search result to store true/false

还使用类级变量来获取搜索结果以存储true / false

@Override
        protected void onPostExecute(Void ret){

            // Set you result here
            setIs_loading(my_searh_result);

            try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};

            if ( searchManager.errorOccurred() ){
                //Error
            } else {
                //No Error

            }   
        }

#2


0  

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.

您可以尝试在布局文件中添加ProgressBar控件,并在启动异步任务时将其Visible设置为true,并在进程结束时设置为off。下面是相同的代码。

<ProgressBar 
android:layout_width="wrap_content" 
android:id="@+id/progressBar1" 
android:layout_height="wrap_content"></ProgressBar>

#3


0  

It's hard to say, but try this for onClickListner:

这很难说,但是为onClickListner试试这个:

button_search.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        new SearchTask().execute();
        setIs_loading(true);
        searchManager.startSearch(); 
    }
});

It's a pretty wild guess, but it might work

这是一个非常疯狂的猜测,但它可能会奏效

#1


0  

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

你需要做的就是把搜索任务的搜索cod ein doInbackGround()。例如

@Override
protected Void doInBackground(Void... params) {

    global.getDataManager().doSearch();
    return null;
}

also use class level variable to get search result to store true/false

还使用类级变量来获取搜索结果以存储true / false

@Override
        protected void onPostExecute(Void ret){

            // Set you result here
            setIs_loading(my_searh_result);

            try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};

            if ( searchManager.errorOccurred() ){
                //Error
            } else {
                //No Error

            }   
        }

#2


0  

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.

您可以尝试在布局文件中添加ProgressBar控件,并在启动异步任务时将其Visible设置为true,并在进程结束时设置为off。下面是相同的代码。

<ProgressBar 
android:layout_width="wrap_content" 
android:id="@+id/progressBar1" 
android:layout_height="wrap_content"></ProgressBar>

#3


0  

It's hard to say, but try this for onClickListner:

这很难说,但是为onClickListner试试这个:

button_search.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        new SearchTask().execute();
        setIs_loading(true);
        searchManager.startSearch(); 
    }
});

It's a pretty wild guess, but it might work

这是一个非常疯狂的猜测,但它可能会奏效