Created
March 14, 2018 08:45
-
-
Save thenixan/fc8e962c2c49377e3e098c5e2cb9e07a to your computer and use it in GitHub Desktop.
LocationListenerExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Acquire a reference to the system Location Manager | |
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); | |
// Define a listener that responds to location updates | |
LocationListener locationListener = new LocationListener() { | |
public void onLocationChanged(Location location) { | |
// Called when a new location is found by the network location provider. | |
makeUseOfNewLocation(location); | |
} | |
public void onStatusChanged(String provider, int status, Bundle extras) {} | |
public void onProviderEnabled(String provider) {} | |
public void onProviderDisabled(String provider) {} | |
}; | |
// Register the listener with the Location Manager to receive location updates | |
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); | |
// Dont' forget to add to AndroidManifest.xml | |
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment