pasek postępu pozostaje pusty przy użyciu asynctask - EDIT - okno dialogowe nigdy nie przestaje się ładować
Chcę mieć pasek postępu podczas oczekiwania na sygnał GPS.
Używam:
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();
}
}
i:
<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" />
Ale pasek postępu pozostaje pusty.
Ponadto, gdy klikam przycisk w celu rozpoczęcia odbierania sygnału GPS, GPS miga, a następnie przestaje migać (ale w międzyczasie nadal szuka sygnału), a gdy go ponownie naciskam, wyświetla mi się komunikat Toast, który mam w onPostExecute . Czy to normalne zachowanie?
Czy ikona nie powinna migać, dopóki nie znajdzie sygnału, a następnie nie pokaże mi wiadomości bez konieczności ponownego naciskania przycisku przez użytkownika?
--------------------------------AKTUALIZACJA----------------- -----------------------------------
Próbowałem także z 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();
}
}
ale teraz okno dialogowe postępu nigdy nie przestaje się ładować. Jeśli nacisnę przycisk Wstecz, a następnie ponownie przycisk, aby uzyskać lokalizację, to otrzymuję wiadomość toastową (z lokalizacją).
-------------------ROZWIĄZANIE------------------------------
Problemem było (po poprawieniu pętli while), że musisz umieścić this.location = location
public void onLocationChanged(Location location) {
this.location=location;
}