0

I want to have a progress bar while waiting for GPS signal to establish.

I use:

public class GetGPSData extends AsyncTask<Void, Integer, Void> {
               @Override
               protected void onPreExecute() {
                   //super.onPreExecute();
                   myprogressBar.setVisibility(View.VISIBLE);
                   myprogressBar.setProgress(0);

               }

               @Override
               protected void onProgressUpdate(Integer... progress) {
                   //super.onProgressUpdate(progress);
                   myprogressBar.setProgress(progress[0]);

               }

               @Override
               protected Void doInBackground(Void... params) {

               latitude = gps.getLatitude();
               longitude = gps.getLongitude();

               while (latitude == 0 || longitude ==0)
               {

                   try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  
               }
                return null;
               }

                  protected void onCancelled() {
                      Toast.makeText(getBaseContext(), "Error connecting", Toast.LENGTH_LONG).show();

                  }

               @Override
               protected void onPostExecute(Void result) {
                   super.onPostExecute(result);
                   myprogressBar.setProgress(100);

               Toast.makeText(getBaseContext(), "Your Location is  \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

               }
           }

and:

 <ProgressBar android:id="@+id/myprogressBar"
  style="?android:attr/progressBarStyleHorizontal" 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:layout_alignParentLeft="true"
  android:indeterminate="true"
  android:layout_below="@+id/comments"
  android:padding="5dp" />

But the progress bar remains empty.

Also,when I click the button in order to start getting the GPS signal, the GPS flashes ,then stop flashing ( but in the meanwhile it still searches for signal ) and when I press it again it gives me the Toast message I have in onPostExecute.Is this normal behavior?

Shouldn't the icon be flashing until finds the signal and then show me the message without the user having to press again the button?

--------------------------------UPDATE----------------------------------------------------

I also tried with ProgressDialog :

public class GetGPSData extends AsyncTask<Void, Integer, Void> {
               ProgressDialog progressDialog = null;               

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



                   progressDialog = new ProgressDialog(ShowList.this);
                   progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            GetGPSData.this.cancel(true);
                        }

                    });
                   progressDialog.setMessage("Waiting for location...");
                   progressDialog.setIndeterminate(true);
                   progressDialog.setCancelable(true);
                   progressDialog.show();


               }

               @Override
               protected void onProgressUpdate(Integer... progress) {
                   super.onProgressUpdate(progress);


               }

               @Override
               protected Void doInBackground(Void... params) {

                   latitude = gps.getLatitude();
                   longitude = gps.getLongitude();


              while  (latitude == 0 || longitude == 0)
               {
                   try {               
                    Thread.sleep(1000);     

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  

               }


                return null;
               }

                  protected void onCancelled() {
                      Toast.makeText(getBaseContext(), "Cancelled/Error connecting", Toast.LENGTH_LONG).show();
                      progressDialog.dismiss();
                  }

               @Override
               protected void onPostExecute(Void result) {

                   progressDialog.dismiss();

 Toast.makeText(ShowList.this, "Your Location is  \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

               }
           }

but now the progress dialog never stops loading.If I press back button and then again the button to get location ,then it gives me the toast message (with location).

-------------------SOLUTION------------------------------

The problem was (after correcting the while loop) that you must put this.location=location

 public void onLocationChanged(Location location) {
        this.location=location;
    }
4

4 回答 4

3

Use publishProgress in your doInBackground method

But in your case, you shouldn't use a horizontal ProgressBar with value because you couldn't know when your longitude and latitude are known, just use a simple ProgressBar.

于 2013-05-16T08:32:17.143 回答
2

Your Progress bar is empty because you didn't call publishProgress() method inside doInBackground() method. publishProgress call onProgressUpdate() method inside your asynctask.

于 2013-05-16T08:32:15.687 回答
1

The reason your progress isn't being published is because you're not calling publishProgress() in your doInBackground function. Also, the reason that your GPS icon stops flashing is because you have a GPS signal. The reason your app says it's still searching for a GPS signal is because your while loop conditions are never updated so your loop never ends. The toast shows up when you press the button again though because a second AsyncTask is being started and it's completing right away.

To fix all of this, you would want to update your doInBackground function to look something like this:

@Override
protected Void doInBackground(Void... params) {

    do {
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();

        /* Not needed for an indeterminate progress bar */
        publishProgress(0);

        try {
            Thread.sleep(1000);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    } while  (latitude == 0 || longitude == 0);

    return null;
}

This will continually look for a gps fix and will break out of the loop properly when a fix is achieved. It will also call your onProgressUpdate to update your progress. The only caveat here is that you need to determine for yourself what you're going to use as progress. I have it in here as just being 0 so you can see where to call it but without knowing what you're using to measure progress, I can't definitively say what will work best for you.

In your case, an indeterminate progress bar might be better than one with a min/max. I see you've done that in your ProgressDialog example so I would just remove the publishProgress line in the above code and go with your second example.

于 2013-05-21T13:53:11.617 回答
1

the onPostExecute callback is invoked when the doInBackground callback runs to end. If it is not called it means that

 while  (latitude == 0 || longitude == 0) {
        try {               
                Thread.sleep(1000);     

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  

       latitude = gps.getLatitude();
       longitude = gps.getLongitude();

  }

 Log.e(getClass().getSimpleName(), "out the while");

neither latitude or longitude value is != 0;

Also if your while loop never ends you will leak your AsyncTask

于 2013-05-22T10:01:21.913 回答