Progress dialog getting dismissed before the thread gets finished - Android
- by user264953
Hi experts,
I use the code provided by Fedor in the following link, in order to get the latitude and longitude from my simple demo app. I am trying to fetch the latitude and longitude using the MyLocation class provided by him in that link.
What is the simplest and most robust way to get the user's current location in Android?
I try to fetch the latitude and longitude on a button click. On the button click, I start an async task and delegate the location fetching work to the do in background method of my asynctask.
pre execute - progressdialog initiated.
post execute - progress dialog dismissed.
This is how, the progress dialog in my code should work and here is the issue which I have. THe progress dialog gets initiated correctly, but even before the latitude and longitude gets printed in the doinbackground method, the progress dialog gets dismissed.
I do not understand why this happens.
Here is my front end activity
public class LocationServices extends Activity {
MyLocation myLocation = new MyLocation();
LocationResult locationResult;
TextView tv1, tv2;
Location location;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new LocationAsyncTasking().execute();
}
});
}
public class LocationAsyncTasking extends AsyncTask<String, Void, Void> {
ProgressDialog dialog;
int totalAvail;
protected void onPreExecute() {
// this.dialog.setMessage("Inserting data...");
dialog = new ProgressDialog(LocationServices.this);
this.dialog.setMessage("Fetching data...");
this.dialog.show();
}
protected Void doInBackground(String... args) {
Looper.prepare();
locationResult = new LocationResult() {
public void gotLocation(Location location) {
// TODO Auto-generated method stub
// LocationServices.this.location = location;
System.out.println("Progress dialog should be present now - latitude"+location.getLatitude());
System.out.println("Progress dialog should be present now - longitude"+location.getLongitude());
}
};
myLocation.getLocation(LocationServices.this, locationResult);
return (null);
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
}
I am quite puzzled, thinking of what makes this progress dialog disappear even before the SOP in doinbackground is finished.
Experts, please help me understand and resolve this issue.
Any help in this regard is well appreciated.
Looking forward,
Best Regards,
Rony