Reputation: 2009
Hey guys so I'm trying to display gps distance between the user and some place on a card they can see. The problem is, I think the main thread splits or does 2 things at once. At the code below, Android makes a toast while doing the code in the if(flag) statement... so it toasts the GPS difference without getting the coordinates of the user...How do i make it so that, it does if(flag) statement first then goes on to do the toast after and outside the if statement?
@Override
public void onResume()
{
super.onResume();
ImagePath = getIntent().getStringExtra("imagePath");
if(ImagePath == null)
{
Toast.makeText(this,"hello everyone",Toast.LENGTH_LONG).show();
}
if(newCardAdded)
{
flag = displayGpsStatus();
if(flag)
{
editLocation.setText("Please!! move your device to" +
" see the changes in coordinates." + "\nWait..");
pb.setVisibility(View.VISIBLE);
locationListener = new MyLocationListener();//LocationListener actually physically gets your location
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
}
else
{
alertbox("Gps Status!!", "Your GPS is: OFF");
}
Global_Class.getInstance().getValue().getlatitude = place_lat;
Global_Class.getInstance().getValue().getlongitutde = place_lon;
String gps_distance = String.valueOf(gps_difference(user_lon, user_lat,place_lon,place_lat));
Toast.makeText(this, gps_distance, Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Views: 74
Reputation: 1792
LocationManager#requestLocationUpdates
Your locationListener
must implement onLocationChanged(Location)
method, which will be called for each location update. Then calculate distance and make a toast in that method. Because requestLocationUpdates(...)
runs in another thread, there's no way to confirm that it is done before Toast.makeText(...)
, so you must use your MyLocationListener
.
Upvotes: 1