When I launch my activity, I want to get the location of the user, using his gps. If the GPS is not on, I display a toast to ask him to turn on his gps to benefit from specific functionality.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.enableAutoManage(this, 34992, this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 50,SelectCinemaActivity.this );
// Define the criteria how to select the locatioin provider -> use default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
setContentView(R.layout.fragment1);
requestLocation();
locationChecker(mGoogleApiClient, Activity.this);
...
}
My requestLocation() function
public void requestLocation(){
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
latitudeActuelle = location.getLatitude();
longitudeActuelle = location.getLongitude();
try {
initList();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
System.out.println("Location not available");
try {
initListWithoutDistance();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
and here the locationChecker with the Toast:
public void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
Log.i("SUCCESS", String.valueOf(status.getStatusCode()));
requestLocation();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
Log.i("RES REQUIRED", String.valueOf(status.getStatusCode()));
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
activity, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i("UNAIVALEBLE", String.valueOf(status.getStatusCode()));
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
try {
initListWithoutDistance();
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
Log.i("IL CLIQUE", "OK");
location = locationManager.getLastKnownLocation(provider);
Log.i("OK CLICK", String.valueOf(location));
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
Log.i("IL CLIQUE", "CANCEL");
try {
initListWithoutDistance();
} catch (JSONException e) {
e.printStackTrace();
}
break;
default:
break;
}
break;
}
}
initList and initListWithoutDistance are 2 function that sort result by distance or name.
When the GPS is active, I got no problem. When the GPS is off, and I click on "OK" button, I can't instantly get the current location. I tried to get the event onLocationChanged by calling location = locationManager.getLastKnownLocation(provider); , but the result is null. I can't call directly the function onLocationChanged because I got no location object.
After several seconds (sometimes 2, sometimes 20...), the event is launched, and the result is tried.
Is there a way to force this event, and to get the current location instantly after the user click on the "OK" Button?