0

I'm developing an app for Android which generates a SMS obtaining the current location of the user. I have done this but the app can't detect the location and it appears the Toast I have created "Fallo2"

I'm testing the app in a real mobile phone.

What is the problem?

Class Main

@Override
    public void onClick(View view) {
    switch (view.getId()) {
    case R.id.enviarMensajeButton:
        if(obtenerTelefonos()){
            if(obtenerLocalizacion()){
                generarMensaje();
            }
            else{
                Toast.makeText(this, "Fallo2", Toast.LENGTH_LONG).show();
            }
        }
        else{
            Toast.makeText(this, "Fallo", Toast.LENGTH_LONG).show();
        }

        break;
    case R.id.menuButton:
        Intent configuracionIntent = new Intent(MainActivity.this, Configuracion.class);
        startActivity(configuracionIntent);
        break;
    }
}

private boolean obtenerTelefonos(){
    return true;
}

private boolean obtenerLocalizacion() {
    GPS gps = new GPS(this);
    int i = 0;
    while(!gps.obtenerLocation()){
        i++;
        if(i > 100){
            return false;
        }
    }
    if(gps.obtenerDireccion()){
        codigoPostal = gps.codigoPostal;
        direccion = gps.direccion;
        ciudad = gps.ciudad;
        pais = gps.pais;
        latitude = String.valueOf(gps.latitude);
        longitude = String.valueOf(gps.longitude);
        return true;
    }
    else{
        return false;
    }
}

private void generarMensaje(){
    tv.setText(codigoPostal+"  "+direccion+"  "+ciudad+"  "+pais+"  "+latitude+"  "+longitude);
}

Class GPS

public class GPS implements LocationListener {
private Context context;
public double latitude, longitude;
public String codigoPostal, direccion, ciudad, pais;
private LocationManager locationManager;
public Location bestLocation;
private List<String> providers;
public List<Address> direcciones;

public GPS(Context context) {
    this.context = context;
    locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
}

public boolean obtenerLocation() {
    try {
        providers = locationManager.getAllProviders();
        bestLocation = null;
        for (String provider : providers) {
            Location location = locationManager.getLastKnownLocation(provider);
            if(location == null) continue;
            if(bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()){
                bestLocation = location;
            }
        }
        if(bestLocation != null){
            return true;
        }
        else{
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

public boolean obtenerDireccion() {
    try {
        // Obtenemos la longitud y la latitud del objeto location

        latitude = bestLocation.getLatitude();
        longitude = bestLocation.getLongitude();

        // Creamos un objeto Geocoder indicando nuestro lenguaje
        //Locale spanish = new Locale("es", "ES");
        Geocoder geocoder = new Geocoder(this.context, Locale.ENGLISH);

        // A partir de Geocoder obtenemos una lista de direcciones
        direcciones = geocoder.getFromLocation(latitude,
                longitude, 1);

        // Obtenemos todos los datos que necesitamos
        codigoPostal = direcciones.get(0).getPostalCode();
        direccion = direcciones.get(0).getAddressLine(0);
        ciudad = direcciones.get(0).getAddressLine(1);
        pais = direcciones.get(0).getAddressLine(2);

        return true;
    } catch (Exception e) {
        return false;
    }
}

@Override
public void onLocationChanged(Location arg0) {

}

@Override
public void onProviderDisabled(String arg0) {

}

@Override
public void onProviderEnabled(String arg0) {

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

}}

I have added the permissions in the manifest file and I need obtaining only one location.

1 Answers1

1

Did you add the correct permissions in the manifest file? you need to post some more code in order to get help generally your

@Override
public void onLocationChanged(Location arg0) {

}

method is empty - that means after the gps signal has been recieved you do nothing with the information - you should use the new location you got to update the user's location, try and use this guid for more guidance you can also get more guidance as for your GPS class from this thread

Community
  • 1
  • 1
crazyPixel
  • 2,301
  • 5
  • 24
  • 48
  • Yes I have added the permissions. I have edited my post at the end. I only need obtaining one location. How can I do it?. Do you mean that the app have wait for the method onLocationChanged? –  Mar 24 '14 at 22:09
  • The onLocationChanged event is fired upon a location change and you get the location (mentioned as arg0) you can extract the lat and lng from this object and update you new location based on this object read the guide I added it might be a but long but I assure you that once you'll finish you'll have all the answers you need. – crazyPixel Mar 24 '14 at 22:12
  • I have read the guide more less but It doesn't content the solution for my problem. I mean I don't know how to "sleep" the app while the gps obtain a good location. –  Mar 24 '14 at 22:21
  • I cant understand what you are trying to achieve bro... if you want to update the location use the onLocationchanged that basically solves your problem... – crazyPixel Mar 24 '14 at 22:26