doInBackground調用AsyncTask然后休眠阻止UI線程

[英]doInBackground calling AsyncTask then sleeps blocks UI Thread


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

這是一個非常瘋狂的猜測,但它可能會奏效


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/11/16/6f51cb1f9e5fb69001b89457093aff4b.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com