首先,我建議您在Async Class中進行所有聯網。 您可以使用以下模板將代碼放入Async Class中。 將ProgressDialog作爲類變量。
YouClass
{
ProgressDialog dialog;
onCreate(....)
{
//Execute the async task here.
new myNetworkingTask().execute("");
}
private class myNetworkingTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params)
{
//In this method you will do the networking task
httpclient.execute();
}
return "";
}
@Override
protected void onPostExecute(String result) {
//In this method you will hide the progress bar
dialog.dismiss();
}
@Override
protected void onPreExecute() {
//In this method you will display the progress bar.
dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
}
@Override
protected void onProgressUpdate(Void... values) {
}
是httpclient aysnctask? – Broak
你確定你想要一個'ProgressBar'而不是'ProgessDialog'嗎?但你肯定需要一個AsyncTask,[this](http://stackoverflow.com/questions/17527773/displaying-a-progressdialog-while-waiting-for-a-joined-thread)可能會幫助你 –