位置在Android手机中无效

时间:2014-03-24 21:48:09

标签: android geolocation

我正在为Android开发一个应用程序,它会生成一个获取用户当前位置的短信。我做了这个,但应用程序无法检测到位置,看起来我创建的Toast“Fallo2”

我正在用真正的手机测试应用程序。

有什么问题?

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);
}

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) {

}}

我已在清单文件中添加了权限,我只需要获取一个位置。

1 个答案:

答案 0 :(得分:1)

您是否在清单文件中添加了正确的权限? 你需要发布更多代码才能获得帮助 一般来说你的

@Override
public void onLocationChanged(Location arg0) {

}

方法是空的 - 这意味着在收到gps信号后你没有对信息做任何事情 - 你应该使用新的位置来更新用户的位置,尝试使用this guid如需更多指导,您还可以从this主题

获得有关GPS班级的更多指导
相关问题