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

发布于 2024-12-16 02:31:02 字数 2166 浏览 0 评论 0原文

我有一个活动,由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹淡然 2024-12-23 02:31:02

您不需要创建另一个任务,只需通过另一个活动进行搜索即可。

您需要做的就是将搜索代码放入搜索任务的 doInbackGround() 中。例如

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

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

还使用类级别变量来获取搜索结果来存储 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

            }   
        }

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

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

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

also use class level variable to get search result to store 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

            }   
        }
无人问我粥可暖 2024-12-23 02:31:02

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

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

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 
android:layout_width="wrap_content" 
android:id="@+id/progressBar1" 
android:layout_height="wrap_content"></ProgressBar>
赠意 2024-12-23 02:31:02

很难说,但是尝试一下 onClickListner:

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

这是一个非常疯狂的猜测,但它可能会起作用

It's hard to say, but try this for 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文