Just use this code :
public class Location extends AppCompatActivity {
LocationManager locationManager;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mContext=this;
locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
2000,
10, locationListenerGPS);
isLocationEnabled();
}
LocationListener locationListenerGPS=new LocationListener() {
@Override
public void onLocationChanged(android.location.Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
String msg="New Latitude: "+latitude + "New Longitude: "+longitude;
Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
protected void onResume(){
super.onResume();
isLocationEnabled();
}
private void isLocationEnabled() {
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
alertDialog.setTitle("Enable Location");
alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu.");
alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
AlertDialog alert=alertDialog.create();
alert.show();
}
else{
AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
alertDialog.setTitle("Confirm Location");
alertDialog.setMessage("Your Location is enabled, please enjoy");
alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
AlertDialog alert=alertDialog.create();
alert.show();
}
}
}
The parameters of requestLocationUpdates methods are as follows:
provider:The name of the provider with which we would like to register.
minTime:Minimum time interval between location updates (in milliseconds).
minDistance:Minimum distance between location updates (in meters).
listener:A LocationListener whose onLocationChanged(Location) method will be called for each location update.
Permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Add above permissions to manifest file for the version lower than lollipop and for marshmallow and higher version use runtime permission.
Answer from user3164401 on Stack OverflowGoogle
developers.google.com › google play services › locationlistener
LocationListener | Google Play services | Google for Developers
October 31, 2024 - LocationListener is an interface for receiving locations from the FusedLocationProviderClient · It is easier to implement and use compared to LocationCallback for most simple location use cases
Android Developers
developer.android.com › api reference › locationlistener
LocationListener | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Videos
Android Developers
stuff.mit.edu › afs › sipb › project › android › docs › reference › android › location › LocationListener.html
LocationListener - Android SDK | Android Developers
Used for receiving notifications from the LocationManager when the location has changed. These methods are called if the LocationListener has been registered with the location manager service using the requestLocationUpdates(String, long, float, LocationListener) method.
Vanderbilt
dre.vanderbilt.edu › ~schmidt › android › android-4.0 › out › target › common › docs › doc-comment-check › reference › android › location › LocationListener.html
LocationListener | Android Developers
Used for receiving notifications from the LocationManager when the location has changed. These methods are called if the LocationListener has been registered with the location manager service using the requestLocationUpdates(String, long, float, LocationListener) method.
Uplandsoftware
help.uplandsoftware.com › localytics › dev › android-javadoc › com › localytics › android › LocationListener.html
LocationListener (android-client-library API)
public interface LocationListener · An interface used to receive location callbacks. void localyticsDidUpdateLocation(@Nullable android.location.Location location) Callback that the LocationManager has received an update · Parameters: location - Location object containg the new location ...
Java Tips
javatips.net › api › android.location.locationlistener
Java Examples for android.location.LocationListener
/** * Registers a listener for real locations * @param listener */ public void registerRealLocationListener(LocationListener listener) { // Check if the GPS provider exists if (myTracksLocationManager.getProvider(LocationManager.GPS_PROVIDER) == null) { listener.onProviderDisabled(LocationManager.GPS_PROVIDER); unregisterLocationListener(listener); return; } // Listen for GPS location myTracksLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); // Update the listener with the current provider state if (myTracksLocationManager.isProviderEnabled(LocationManager.G
Google
android.googlesource.com › platform › frameworks › base › + › master › location › java › android › location › LocationListener.java
location/java/android/location/LocationListener.java - platform/frameworks/base - Git at Google
android / platform / frameworks / base / refs/heads/main / . / location / java / android / location / LocationListener.java
DigitalOcean
digitalocean.com › community › tutorials › android-location-api-tracking-gps
Android Location API to track your current location | DigitalOcean
August 3, 2022 - The LocationListener interface, which is part of the Android Locations API is used for receiving notifications from the LocationManager when the location has changed. The LocationManager class provides access to the systems location services.
Javased
javased.com › index.php
Java Code Examples of android.location.LocationListener
public void startUpdates(boolean instantLastLocation){ status=RUNNING; listeners=new LinkedList<LocationListener>(); LocationListener ll; for (Iterator<String> i=locationManager.getProviders(true).iterator(); i.hasNext(); ) { ll=new LocationListener(){ @Override public void onLocationChanged( Location location){ if (!locked) update(location); } @Override public void onProviderDisabled( String provider){ } @Override public void onProviderEnabled( String provider){ } @Override public void onStatusChanged( String provider, int status, Bundle extras){ } } ; listeners.add(ll); locationManager.requestLocationUpdates(i.next(),60000,25,ll); } if (instantLastLocation) { update(getLastKnownLocation()); } }
Java2s
java2s.com › example › java-api › android › location › locationlistener › locationlistener-0-0.html
Example usage for android.location LocationListener LocationListener
private void requestLocationUpdates() { // 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) { JSONObject locationAsJson = new JSONObject(); try { locationAsJson.put("accuracy", location.getAccuracy()); locationAsJson.put("provider", location.getProvider()); locationAsJson.put("latitude", location.getLatitude()); locationAsJson.put("longitude", location.getLongitude()); locationAsJson.put("time", location.getTime()); } catch (JSONException e) { Log.e(TAG, "JSON exception", e); return; }/*from w w w .j av a2 s .
Codename One
codenameone.com › javadoc › com › codename1 › location › LocationListener.html
LocationListener (Codename One API)
public interface LocationListener · This is a Listener to the Locations events see LocationManager.setLocationListener · void locationUpdated(Location location) This method is been called by the system when Location is being updated · Parameters: location - a Location Object ·
Situm
developers.situm.com › sdk_documentation › android › javadoc › 2.5.0 › es › situm › sdk › location › LocationListener.html
LocationListener (Situm Android SDK 2.5.0)
public interface LocationListener · Used for received locations and statuses from the LocationManager. You have to register the listener using the LocationManager.requestLocationUpdates(LocationRequest, LocationListener) void onLocationChanged(Location location) Called when the location has ...
Amazon Web Services
s3-eu-west-1.amazonaws.com › steerpath › android › documentation › latest › javadoc › reference › com › steerpath › sdk › location › LocationListener.html
LocationListener - Steerpath SDK | Steerpath SDK for Android
implements LocationListener · The listener interface for receiving Location updates. Called when accuracy has deteriorated. It means that positioning has not been yet lost, but no positioning updates were received in time. Called when Location couldn't be calculated.