如何从AsyncTask显示和隐藏活动的ProgressBar?(How to show and hide an activity's ProgressBar from a AsyncTas

编程入门 行业动态 更新时间:2024-10-24 16:28:23
如何从AsyncTask显示和隐藏活动的ProgressBar?(How to show and hide an activity's ProgressBar from a AsyncTask?)

我有一个创建了一个单独的类,它扩展了我在整个应用程序中用于通信的AsyncTask。 我的类的每个活动都使用此AsyncTask类进行通信。 我想在通信进行时显示ProgressBar(活动指示符)。 如果我在使用通信类的活动中显示和隐藏ProgressBar,则ProgressBar会冻结,直到通信完成。 有没有办法在AsyncTask类的屏幕中间显示ProgressBar,因此它不会冻结,并且用户知道后台发生了某些事情。 我使用以下代码:

public class MyActivity extends Activity { ProgressBar spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity_layout); spinner = (ProgressBar)findViewById(R.Id.progressbar); spinner.setVisibility(ProgressBar.INVISIBLE); } protected void onResume() { super.onResume(); } public void buttonClickHandler(View target){ try{ spinner.setVisibility(ProgressBar.VISIBLE); String output = new SendRequest().execute().get(); spinner.setVisibility(ProgressBar.INVISIBLE); }catch(Exception ex){ System.out.println("buttonClickHandler exception: "+ex); } } }

和AsyncTask类是:

public class SendRequestExample extends AsyncTask<ArrayList<String>, Void, String> { public String result; protected String doInBackground(ArrayList<String>... params) { // doing communication ... return result; } }

提前致谢。

I have a created a separate class which extends AsyncTask which i am using for communication in my whole app. Every activity of my class uses this AsyncTask class for communication. I want to show ProgressBar (activity indicator) whenever communication is in progress. If i show and hide ProgressBar in activity which uses communication class then ProgressBar freezes until communication is done. Is there any way to show a ProgressBar in middle of screen from AsyncTask class so it doesn't freeze and user knows that there is something going on in background. I am using following code:

public class MyActivity extends Activity { ProgressBar spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity_layout); spinner = (ProgressBar)findViewById(R.Id.progressbar); spinner.setVisibility(ProgressBar.INVISIBLE); } protected void onResume() { super.onResume(); } public void buttonClickHandler(View target){ try{ spinner.setVisibility(ProgressBar.VISIBLE); String output = new SendRequest().execute().get(); spinner.setVisibility(ProgressBar.INVISIBLE); }catch(Exception ex){ System.out.println("buttonClickHandler exception: "+ex); } } }

and AsyncTask class is:

public class SendRequestExample extends AsyncTask<ArrayList<String>, Void, String> { public String result; protected String doInBackground(ArrayList<String>... params) { // doing communication ... return result; } }

Thanks in advance.

最满意答案

您应该在以下位置显示进度条:

SendRequestExample.onPreExecute()

并将其隐藏在:

SendRequestExample.onPostExecute()

方法。

如果您的SendRequestExample对于许多活动是通用的,那么您可以使MyActivity实现接口,即:

interface ProgressBarAware { void showProgressbar(); void hideProgressbar(); }

并将ProgressBarAware引用传递给您的AsyncTask。 在配置更改期间更新AsyncTask中的此引用很重要,这意味着每次Activity.onCreate在配置更改后执行。


还有一个:不要在UI线程的asynctask上使用get(),在调用'new SendRequest()。execute()。get();'。 这会导致很多问题。 它阻止消息队列,Android用它来处理负责响应用户点击,绘制小部件等的消息。 几分钟后,这样的UI线程阻塞将在ANR(应用程序没有响应)中结束,系统将终止您的应用程序。

You should show your progress bar in :

SendRequestExample.onPreExecute()

and hide it in:

SendRequestExample.onPostExecute()

methods.

If your SendRequestExample is universal for many activities, then you can make MyActivity implement interface, ie:

interface ProgressBarAware { void showProgressbar(); void hideProgressbar(); }

and pass ProgressBarAware reference to your AsyncTask. Its important to update this reference in AsyncTask during configuration changes, that means every time your Activity.onCreate executes after config change.


One more: dont use get() on asynctask on UI thread, in call 'new SendRequest().execute().get();'. This causes lots of problems. It block message queue that is used by Android to process messages responsible for reacting to user clicks, drawing widgets, and lots more. After few minutes, such UI thread blocking will end up in ANR (Application Is Not Responding), and system will terminate your app.

更多推荐

本文发布于:2023-08-06 03:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ProgressBar   AsyncTask   show   activity   hide

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!