我有一个创建了一个单独的类,它扩展了我在整个应用程序中用于通信的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.
更多推荐
发布评论