1

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();
}
vektor
  • 3,312
  • 8
  • 41
  • 71
user420574
  • 1,437
  • 5
  • 21
  • 33

1 Answers1

0

Android devices don't necessarily automatically refresh location information when the user enables the location feature in their phone.

To guarantee that you get some kind of location back, you'll need to register a LocationListener for a single or multiple updates.

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0.0f, this);

And in your main class that is "this", add implements LocationListener, and add the below methods:

public void onLocationChanged(Location location) {
    //This "location" object is what will contain updated location data
    //when the listener fires with a location update
}

public void onStatusChanged(String provider, int status, Bundle extras) {
    //Required by LocationListener - you can do nothing here
}

public void onProviderEnabled(String provider) {
    //Required by LocationListener - you can do nothing here
}

public void onProviderDisabled(String provider) {
    //Required by LocationListener - you can do nothing here
}

When you've gotten a location update, you can disable the listener by:

locationManager.removeUpdates(this);

More documentation on LocationListener here: http://developer.android.com/reference/android/location/LocationListener.html

Sean Barbeau
  • 11,496
  • 8
  • 58
  • 111
  • I''m trying your approach but there is some problem. Please have a look: http://stackoverflow.com/q/41206885/6144372 – Hammad Nasir Dec 18 '16 at 09:35