Android LocationManager Code Example

Android Documentation for LocationManager:
This class provides access to the system location services. These services allow applications to obtain periodic updates of the device’s geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

You do not instantiate this class directly; instead, retrieve it through Context.getSystemService(Context.LOCATION_SERVICE).

LocationManager Code Example:

In Manifest.xml, add ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

In the Activity file, for example MainActivity.java, add the following to the onCreate method. It initializes the LocationManager, set the Criteria, get the location and add the location to the MyLocationListener. MyLocationLister extends from LocationListener, it will be defined as an inner class in the Activity.

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setCostAllowed(false);

String provider = locationManager.getBestProvider(criteria, false);
Location location = null;
try {
    location = locationManager.getLastKnownLocation(provider);

    MyLocationListener mylistener = new MyLocationListener();

    if (location != null) {
        // add location to the location listener for location changes
        mylistener.onLocationChanged(location);
    } else {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    // location updates: at least 1 meter and 500 milli seconds change
    locationManager.requestLocationUpdates(provider, 500, 1, mylistener);
} catch (SecurityException e) {
    Log.e("SecurityException", e.getMessage());
}

Lastly, add the MyLocationListener in the Activity class.

private class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        // Do something with the location
        Toast.makeText(getBaseContext(),  "Location changed!", Toast.LENGTH_SHORT).show();
        Log.i("Provider: ", location.getProvider());
        Log.i("Latitude: ", String.valueOf(location.getLatitude()));
        Log.i("Longitude: ", String.valueOf(location.getLongitude()));

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i("onStatusChanged: ",  "Do something with the status: " + status );
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.i("onProviderEnabled: ", "Do something with the provider-> " + provider);
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.i("onProviderDisabled:", "Do something with the provider-> " + provider);
    }
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search