I want to click a button and receive the current location, i understand that i can't instantly get the location , so this is what i did :
the click event :
public void onClick(View v)
{
ProgressDialog MyDialog = ProgressDialog.show( MainPage.this, " " , " Loading. Please wait ... ", true);
MyActionsHandler myActionsHandler = new myActionsHandler(MainPage.this);
myActionsHandler.startSearch();
MyDialog.dismiss();
Intent intent = new Intent(MainPage.this, ResultPage.class);
startActivity(intent);
}
and this is the handler that searches for the location
public void startSearch(long timeInterval,float distanceInterval)
{
LocationManager lm = (LocationManager)_context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval,
distanceInterval, this);
while(!_locationFound)
{
//wait till location is found
}
}
public void onLocationChanged(Location location)
{
if (location != null)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float speed = location.getSpeed();
float bearing = location.getBearing();
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
try
{
doTheProcess(_searchType,latitude, longitude, speed, bearing);
_locationFound = true;
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I understand that this doesn't work, because the loop is in the same thread,
so what do you suggest the best solution to do it?
in the javadoc of requestLocationUpdates , there is "The calling thread must be a Looper thread such as the main thread of the calling Activity." but i haven't found any example so i don't know if it's the right solution.
one more question,
does the getLastKnownLocation()
work even i fi never called the locationManager before?
thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…