Try using LocationRequest.Builder
Below code for creating location request
Kotlin
LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
.apply {
setWaitForAccurateLocation(false)
setMinUpdateIntervalMillis(IMPLICIT_MIN_UPDATE_INTERVAL)
setMaxUpdateDelayMillis(100000)
}.build()
Java
new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
.setWaitForAccurateLocation(false)
.setMinUpdateIntervalMillis(IMPLICIT_MIN_UPDATE_INTERVAL)
.setMaxUpdateDelayMillis(100000)
.build()
Read more about here https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder
Answer from yoman796 on Stack OverflowYes, the LocationRequest constructor is deprecated. You can use its static method LocationRequest.create() to create a location request.
Kotlin:
locationRequest = LocationRequest.create().apply {
interval = 100
fastestInterval = 50
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
maxWaitTime = 100
}
Java:
locationRequest = LocationRequest.create()
.setInterval(100)
.setFastestInterval(3000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setMaxWaitTime(100);
Update
As @Shimon pointed out LocationRequest.PRIORITY_HIGH_ACCURACY is now deprecated, so instead use Priority.PRIORITY_HIGH_ACCURACY
LocationRequest.create().apply{ ... } is now also deprecated.
Please use LocationRequest.Builder() instead. I.E. like this:
(locationInterval, locationFastestInterval and locationMaxWaitTime corresponds to the values used before when using create())
locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, locationInterval)
.setWaitForAccurateLocation(false)
.setMinUpdateIntervalMillis(locationFastestInterval)
.setMaxUpdateDelayMillis(locationMaxWaitTime)
.build()
Please read more here: https://developer.android.com/reference/android/location/LocationRequest.Builder
and here: https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder
LocationRequest locationRequest =
new LocationRequest.Builder(
LocationRequest.PRIORITY_HIGH_ACCURACY,
10000
).build();
locationRequest.setFastestInterval(5000);
locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 100)
.setIntervalMillis(INTERVAL_MILLIS) // Sets the interval for location updates
.setMinUpdateIntervalMillis(INTERVAL_MILLIS/2) // Sets the fastest allowed interval of location updates.
.setWaitForAccurateLocation(false) // Want Accurate location updates make it true or you get approximate updates
.setMaxUpdateDelayMillis(100) // Sets the longest a location update may be delayed.
.build();
I got the same problem, and I used LocationRequest.Builder to create my locationRequest then, GPS signal became stable:
locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000)
.setWaitForAccurateLocation(false)
.setMinUpdateIntervalMillis(500)
.setMaxUpdateDelayMillis(1000)
.build();
If Google Maps isn't working either, there's no chance you can get your app working properly. The device is broken or misconfigured. You could reset it to factory settings and try again.
Maybe you shouldn't use null as third parameter of requestLocationUpdates(). I've used Looper.getMainLooper() like Google says. https://developer.android.com/training/location/request-updates
Error: Exception e is instantiated as "ApiException" so casting to ResolveApiException(subclass) is not possible.
e.getClass();//returnsString ApiException instead of ResolveApiException
A possible workaround:use ResolvableApiException constructor,requires only status code.
ResolvableApiException resolvable = new ResolvableApiException(exception.getStatus());
//use ResolvableApiException Constructor
//public ResolvableApiException (Status status)
Credit to Gui_user for the solution.
In kotlin you can do like this on the fly instead of doing a cast to ResolvableApiException:
ResolvableApiException(exception.status).startResolutionForResult(this, REQUEST_CHECK_SETTINGS)