Android LocationManager using getLastKnownLocation () value is not obtained

2

I make this query, even before it worked and I think (at least I think) have not deleted anything but I stop working.

Can not find the location of the device to store latitude and longitude. The permissions in the Manifesto are, I'm telling you yesterday it worked for me and suddenly I stop doing it

    ActivityCompat.requestPermissions(UltimoPaso.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 120);

if (ActivityCompat.checkSelfPermission(UltimoPaso.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
    Log.e("Permisos: ", "No se han definido los permisos necesarios");
}else
{
    /*Se asigna a la clase LocationManager el servicio a nivel de sistema a partir del nombre.*/
   locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}

if (loc != null) {
   Log.e("Latitud: ", (String.valueOf(loc.getLatitude())));
   Log.e("Longitud: ", (String.valueOf(loc.getLongitude())));
   Log.e("Altitud: ", (String.valueOf(loc.getAltitude())));
   Log.e("Precision: ", (String.valueOf(loc.getAccuracy())));
   latitude_string = (String.valueOf(loc.getLatitude()));
   longitude_string = (String.valueOf(loc.getLongitude()));
 }else {
   latitude_string = "";
   longitude_string = "";

   Toast.makeText(getApplicationContext(), "Ocurrio un error al capturar la ubicacion. No se guardo la ubicacion", Toast.LENGTH_LONG).show();

}

Enter the ELSE, that is, the permissions are there, but the result is that the variable loc = null

    
asked by desarrollosTELLO 01.08.2018 в 16:54
source

2 answers

0

In this case you mention that yesterday it worked, I see that you are using only the GPS provider:

loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

First it is important to verify that you have the geolocation services activated otherwise you will not be able to obtain data.

and since you have only ACCESS_FINE_LOCATION defined, you must ensure that the option of "high precision", if it is not activated that may be the reason why it does not work for you today and loc has a value null :

I recommend defining the following permissions in your AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

and require them manually:

 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.e(TAG, "Permission not granted!");
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                    125);

        }

Remember that the method getLastKnownLocation () requires permits ACCESS_COARSE_LOCATION for a rough location and ACCESS_FINE_LOCATION for accurate location.

    
answered by 01.08.2018 в 17:51
0

already tried adding the permission of coarse location in the following way?

if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 100); } else displayLocationSettingsRequest();

    
answered by 01.08.2018 в 17:26