简体   繁体   中英

Location doesn't work in my mobile with Android

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM