Everytime a user open the app, we check if we got his current location. If not, the app asks him to enable location in the LocationManager
and come back on the app. The problem: sometimes, in certain phones, even after the location is enabled and user comes back to the app, the location is still null
. So the user is stuck in a loop. Why the location is still null? What can I do?
String locationContext = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(locationContext);
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
final String lat = String.valueOf(latitude);
final String lon = String.valueOf(longitude);
System.out.println("Localisation: " + lat + " " + lon);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String id = preferences.getString("id", null);
new sendLocation().execute(id, lat, lon);
} else {
System.out.println("NO LOCATION!!");
AlertDialog.Builder alert = new AlertDialog.Builder(Home.this);
alert.setTitle("Get started");
alert.setMessage("We need your location to detect places nearby. Please enable -Wireless Networks- in your location settings to get started.");
// Set an EditText view to get user input
final TextView input = new TextView(Home.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}