使用AsyncTask隐藏/显示ProgressBar

时间:2016-08-18 09:54:31

标签: java android xml android-asynctask

我正在尝试根据AsyncTask状态执行隐藏/显示ProgressBar的简单操作, 我有两个类,一个扩展FragmentActivity和第二个AsyncTask。

MainActivity.java

public class MainActivity extends FragmentActivity {

public static ProgressBar mProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
}

@Override
protected void onStart() {
    super.onStart();
    // What will happen to the progress bar here?  
}

@Override
protected void onStop() {
    super.onStop();
  // What will happen to the progress bar here? 
}

       @Override
        protected void onResume() {
        super.onResume();

      // What will happen to the progress bar here? 
    }
    }

MyAsyncTask.java

public class MyAsyncTask extends AsyncTask<Void,Void, Void> {

@Override
protected Void doInBackground() {
    // start download some images from cloud 
    // Here the progress bar should start to appear in MainActivity
    // mProgressBar.setVisibility(View.VISIBLE);

}

@Override
protected void onPostExecute(Void result) {
    Log.d(TAG, "Finished book downloading images the cloud");
   // Here the progress bar should start to disappear in MainActivity
   // mProgressBar.setVisibility(View.GONE);


}

}

main_activity.xml

      <ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true" />

希望你理解我,感谢所有可以提供帮助的人。

4 个答案:

答案 0 :(得分:1)

public class MyAsyncTask extends AsyncTask<Void,Void, Void> {
@Override
protected void onPreExecute(Void result) {
    Log.d(TAG, "Finished book downloading images the cloud");
   // Here the progress bar should start to disappear in MainActivity
    mProgressBar.setVisibility(View.VISIBLE);


}
@Override
protected Void doInBackground() {
// start download some images from cloud 
// there is a time cost operation


}

@Override
protected void onPostExecute(Void result) {
    Log.d(TAG, "Finished book downloading images the cloud");
   // Here the progress bar should start to disappear in MainActivity
   // mProgressBar.setVisibility(View.GONE);


}

}

您应该使用onPreExecute()方法来显示对话框。

这是MainActivity

@Override
protected void onResume() {
    super.onResume();
    new MyAsyncTask().execute();
}

我认为你应该先阅读APIs

答案 1 :(得分:1)

进度条应出现在 onPreExecute()方法中(仍在UI线程中)。然后当你回到 onPostExecute 方法中的UI线程时,你会忽略它。

答案 2 :(得分:0)

在UI线程上运行时使用preExecute和postExecute方法。

public class MyAsyncTask extends AsyncTask<Void,Void, Void> {

ProgressBar pBar;
@Override
protected void onPreExecute(Void result) {
pBar=new ProgressBar(getContext());
pBar.show();
}

@Override
protected void onPostExecute(Void result) {
   if(pBar !=null and pBar.isShowing()){
  pBar.dismiss();
}
}

}

答案 3 :(得分:0)

使用Asyntask类中的接口与Activity类进行通信

startProgress();
    MyAsyncTask mTask=new MyAsyncTask(YourActivity.this);
    mTask.setmCallBack(new MyAsyncTask.MyAsyncCallBack() {
        @Override
        public void onSuccess(String successMessage) {
            //do success work and hide progress
            hideProgress();
        }

        @Override
        public void onError(String successMessage) {
            //do error work and hide progress
            hideProgress();
        }
    });
    mTask.execute();

}

从活动类调用AsynckTask类。在调用asyntask之前启动进度。并通过界面完成asyntask返回活动的工作并隐藏进度。

&quote;<b>Demo</b>&quote; &amp;Hase&gt;