I have the following code and it's not good because sometimes GPS takes very long
How can I do the following:
I can do this with my own logic using a time or Thread.sleep but I think there might be a more stabndard way
// 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.
locationCallback(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.GPS_PROVIDER, 0, 0, locationListener);
In RESOLUTION_REQUIRED case, we ask user to give permissions, this is where we show the user prompt to enable GPS. Task<LocationSettingsResponse> result = LocationServices. getSettingsClient(getActivity()).
Get current location settingsTask<LocationSettingsResponse> task = client. checkLocationSettings(builder. build()); When the Task completes, your app can check the location settings by looking at the status code from the LocationSettingsResponse object.
There's no standard way, you have to do it on your own with the help of:
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//Do what you need if enabled...
}else{
//Do what you need if not enabled...
}
And this permission in manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
As recommendation if GPS is not enabled, usually the standard specifies to popup the Location Settings Activity so the user can specifically enable it...
Hope this helps.
Regards!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With