Reputation: 6138
I am building an application that uses GPS to determine users current country. Now, since GPS takes a while to find the location, while it is searching i want to show a ProgressDialog.
So inside my LocationListener, I called a ProgressDialog that i declared before called
pd
And right underneath the part where it shows i created a thread to do the GPS stuff. for some reason, the ProgressDialog isn't showing :/
Here is my code:
pd = ProgressDialog.show(TipCalculatorActivity.this, "", "Looking for GPS satellites...");
new Thread()
{
public void run()
{
try
{
Geocoder geocoder = new Geocoder(TipCalculatorActivity.this, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
countryCode = addresses.get(0).getAddressLine(2);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
finish();
}
if(countryCode==null){
Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
finish();
}
Toast.makeText(TipCalculatorActivity.this, countryCode, Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progressdialog
pd.dismiss();
}
}.start();
What is wrong here?!
Thanks!
EDIT
My new code: Where i call the AsyncTask:
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
lat = loc.getLatitude();
lng = loc.getLongitude();
MyAsyncTask task = new MyAsyncTask(TipCalculatorActivity.this, lng, lat);
task.execute();
Toast.makeText(TipCalculatorActivity.this, "Done :D", Toast.LENGTH_LONG).show();
}
The AsyncTask:
public class MyAsyncTask extends AsyncTask<Void, Void, Result>{
private Activity activity;
private ProgressDialog progressDialog;
private double longitude;
private double latitude;
private String countryCode;
public MyAsyncTask(Activity activity, double longitude, double latitude) {
super();
this.activity = activity;
this.longitude = longitude;
this.latitude = latitude;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog.show(activity, "", "Looking for GPS satellites...");
}
@Override
protected Result doInBackground(Void... v) {
Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
countryCode = addresses.get(0).getAddressLine(2);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
}
if(countryCode==null){
Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
}
Toast.makeText(activity, countryCode, Toast.LENGTH_LONG).show();
return null;
}
@Override
protected void onPostExecute(Result result) {
progressDialog.dismiss();
Toast.makeText(activity.getApplicationContext(), "Finished.",
Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Views: 221
Reputation: 93872
If I were you, I will use an AsyncTask for that :
public class MyAsyncTask extends AsyncTask<Void, Void, Result>{
private Activity activity;
private ProgressDialog progressDialog;
private long longitude;
private long latitute;
public MyAsyncTask(Activity activity, Long longitude, Long latitude) {
super();
this.activity = activity;
this.longitude = longitude;
this.latitude = latitude;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
}
@Override
protected Result doInBackground(Void... v) {
//do your stuff here
return null;
}
@Override
protected void onPostExecute(Result result) {
progressDialog.dismiss();
Toast.makeText(activity.getApplicationContext(), "Finished.",
Toast.LENGTH_LONG).show();
}
}
Call it from the activity:
MyAsyncTask task = new AsyncTask(myActivity.this, lng, lat);
task.execute();
Upvotes: 1