i am working on sample application.In this application i would like to get updated location latitude and longitude when a user moving with android mobile on a way.I have implemented Location Manager class as follows :
private LocationManager locationManager;
private LocationListener locationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}
private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
}
}
}
How to get updated location latitude and longitude from back ground ?
please any body help me.
If you are intending to get the latitude and longitude while your application is not running, you can use a Service which get the latitude and longitude in background and do whatever task you want to. And also don’t forget to remove updates when not needed because getting updates is very costly operation in terms of device battery use.
One more thing, you don’t need to check the location for null value because onLocationChanged() will be called only when a location is got by the your provider.
Although I am also new to android. This may help you. You must see the android documentation what these Service class methods actually do and when they are called.
This is not completed code. You have implement the locationlistener in this yourself. This is just an example program which shows a normal class: