I have an app with 3 activities. MainActivity is the initial screen that is shown to the user. SecondActivity is an activity that allows a user to scan a QR code. After the code is scanned, ThirdActivity is started.
That ThirdActivity shows the GPS location so that I can use it later in the app.
To deal with the location, I have created this helper class:
public class LocationHelper {
private static GeoLocation mLocation;
private static LocationRequest mLocationRequest;
private static long UPDATE_INTERVAL = 10 * 1000; /* 10 secs */
private static long FASTEST_INTERVAL = 2000; /* 2 sec */
public static void start(Context context)
{
startLocationUpdates(context);
getLastLocation(context);
}
@SuppressLint("MissingPermission")
protected static void startLocationUpdates(Context context) {
// Create the location request to start receiving updates
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(context);
settingsClient.checkLocationSettings(locationSettingsRequest);
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
getFusedLocationProviderClient(context).requestLocationUpdates(mLocationRequest, new LocationCallback() {
@override
public void onLocationResult(LocationResult locationResult) {
// do work here
onLocationChanged(locationResult.getLastLocation());
}
},
Looper.myLooper());
}
public static GeoLocation getLocation() {
return mLocation == null ? new GeoLocation() : mLocation;
}
protected static void onLocationChanged(Location location) {
if (mLocation == null)
mLocation = new GeoLocation();
mLocation.setLatitud(location.getLatitude());
mLocation.setLongitud(location.getLongitude());
}
@SuppressLint("MissingPermission")
protected static void getLastLocation(Context context) {
// Get last known recent location using new Google Play Services SDK (v11+)
FusedLocationProviderClient locationClient = getFusedLocationProviderClient(context);
locationClient.getLastLocation()
.addOnSuccessListener(location -> {
// GPS location can be null if GPS is switched off
if (location != null) {
onLocationChanged(location);
}
})
.addOnFailureListener(e -> {
Log.d("LocationHelper", "Error trying to get last GPS location");
e.printStackTrace();
});
}
}
In MainActivity onCreate method I am calling `LocationHelper.start(this);` so that location starts to be getting.
In ThirdActivity onCreate method I cam calling `GeoLocation location = LocationHelper.getLocation();`.
The problem is that on some execution, `getLocation` retrieves the location correctly, but on other execution, `getLocation` sets longitude and latitude both to 0.
What is the problem with my code?
That ThirdActivity shows the GPS location so that I can use it later in the app.
To deal with the location, I have created this helper class:
public class LocationHelper {
private static GeoLocation mLocation;
private static LocationRequest mLocationRequest;
private static long UPDATE_INTERVAL = 10 * 1000; /* 10 secs */
private static long FASTEST_INTERVAL = 2000; /* 2 sec */
public static void start(Context context)
{
startLocationUpdates(context);
getLastLocation(context);
}
@SuppressLint("MissingPermission")
protected static void startLocationUpdates(Context context) {
// Create the location request to start receiving updates
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(context);
settingsClient.checkLocationSettings(locationSettingsRequest);
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
getFusedLocationProviderClient(context).requestLocationUpdates(mLocationRequest, new LocationCallback() {
@override
public void onLocationResult(LocationResult locationResult) {
// do work here
onLocationChanged(locationResult.getLastLocation());
}
},
Looper.myLooper());
}
public static GeoLocation getLocation() {
return mLocation == null ? new GeoLocation() : mLocation;
}
protected static void onLocationChanged(Location location) {
if (mLocation == null)
mLocation = new GeoLocation();
mLocation.setLatitud(location.getLatitude());
mLocation.setLongitud(location.getLongitude());
}
@SuppressLint("MissingPermission")
protected static void getLastLocation(Context context) {
// Get last known recent location using new Google Play Services SDK (v11+)
FusedLocationProviderClient locationClient = getFusedLocationProviderClient(context);
locationClient.getLastLocation()
.addOnSuccessListener(location -> {
// GPS location can be null if GPS is switched off
if (location != null) {
onLocationChanged(location);
}
})
.addOnFailureListener(e -> {
Log.d("LocationHelper", "Error trying to get last GPS location");
e.printStackTrace();
});
}
}
In MainActivity onCreate method I am calling `LocationHelper.start(this);` so that location starts to be getting.
In ThirdActivity onCreate method I cam calling `GeoLocation location = LocationHelper.getLocation();`.
The problem is that on some execution, `getLocation` retrieves the location correctly, but on other execution, `getLocation` sets longitude and latitude both to 0.
What is the problem with my code?