Progress dialog getting dismissed before the thread gets finished - Android
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 on 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.pr开发者_StackOverflowintln("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
I found all of the examples rather confusing being new to Android programming, in the end ... the simplest was the one that worked, so this answer is based on the comment above that has the code pasted into it, but I've attached my full activity which works .. in that the labels are updated with the location after you pressed the button. Thought it make someone's life easier. Oh, and make sure you use the MyLocation class as mentioned above.
package com.benderapp;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.benderapp.MyLocation.LocationResult;
public class DefaultActivity extends Activity {
MyLocation myLocation = new MyLocation();
TextView tv1, tv2;
Button btn;
@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);
btn = (Button)findViewById(R.id.Button01);
final MyLocation myLocation = new MyLocation();
final LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(Location location) {
// TODO Auto-generated method stub
tv1.setText(location.getLatitude() + "");//need to add latitude i get here
tv2.setText(location.getLongitude() + "");//need to add longitude i get here
}
};
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myLocation.getLocation(DefaultActivity.this,locationResult);
}
});
}
}
Something like that. Just add ProgressDialog if you need it.
public class LocationServices extends Activity {
MyLocation myLocation = new MyLocation();
TextView tv1, tv2;
@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) {
myLocation.getLocation(LocationServices.this, locationResult);
}
});
}
public LocationResult locationResult = new LocationResult() {
public void gotLocation(Location location) {
runOnUiThread(new Runnable(){
@Override
public void run(){
tv1.setText(location.getLatitude());
tv2.setText(location.getLongitude());
}
});
}
};
}
精彩评论