1

I'm writing an android application and I want when httpclient.execute() is being executed, the screen go dark and a progress bar appear until this line is executed, what should I do? my code is like this :

ProgressBar x = (ProgressBar) findViewById("R.id.progress") ;
x.setVisibility(ProgressBar.VISIBLE) ;
httpclient.execute()
x.setVisibility(ProgressBar.GONE) ;

but with this code only the progress bar appears and the screen doesn't become dark.

4

3 回答 3

4

Firstly, i recommend that you do all the networking in Async Class. You can use the following template to put your code in Async Class. Take ProgressDialog as a class variable.

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) {
  }
于 2013-07-16T12:05:16.617 回答
0

If your HTTPClient is in an Asynctask, put your progress bar visibility GONE in the 'onPostExecute'

to dim the screen, you need to show an alert dialog/ dialog fragment and set your custom layout with the progress bar, then dismiss it when the loading as finished.

that will automatically dim the screen.

于 2013-07-16T11:52:11.720 回答
0

Add the httpclient.execute() part inside the asynctas..

Add the pretask inside the preexecute method and post task inside postexecute method.

于 2013-07-16T11:55:34.890 回答