Hey guys
I am developing an application in which i need to find the latitude
longitude of the user. I am able to find latitude longitude by network providers through following code :-
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(BaseScreen.this, "Lat: "+loc.getLatitude()+" long: "+loc.getLongitude(), Toast.LENGTH_LONG).show();
Now the following are the cases which i need to implement :-
- What if user is in a building and he can not retrieve data from GPS
- What if the user has GPS switched off and wants to get location from Network
- My application has like 20 activities if i place the code of locationlistener in one base screen which is extended by other activities and keep saving the latitude and longitude in some datamanager then will the call back work in that base screen activity even if the activity is not the main UI activity??
a. Usually we should register for both GPS and network provider while requesting updates, this takes care of the line-of-sight problem where we automatically fall back on network provided values.
b. If the GPS is switched off you can check that from the code by using isProviderEnabled(), if not than your only option is to use the Network Provider.
c. Better still have LocationListener as a separate class and in onLocationChanged store the location received which should be available across your application.
To register use android.location.LocationListener.requestLocationUpdates