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
Lat: " + latitude + "
Long: " + 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
Lat: " + latitude + "
Long: " + 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;
}
See Question&Answers more detail:
os