【问题标题】:Location doesn't work in my mobile with Android位置在我的 Android 手机中不起作用
【发布时间】:2014-05-02 12:15:51
【问题描述】:

我正在为 Android 开发一个应用程序,它会生成一条获取用户当前位置的 SMS。我已经这样做了,但应用程序无法检测到该位置,并且出现了我创建的 Toast “Fallo2”

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

有什么问题?

类主

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

}}

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

【问题讨论】:

    标签: android geolocation


    【解决方案1】:

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

    @Override
    public void onLocationChanged(Location arg0) {
    
    }
    

    method 为空 - 这意味着在接收到 gps 信号后,您对信息不做任何操作 - 您应该使用获得的新位置来更新用户的位置,尝试使用 this guid 获得更多指导,您也可以从this 线程获取有关您的 GPS 课程的更多指导

    【讨论】:

    • 是的,我已经添加了权限。我在最后编辑了我的帖子。我只需要获得一个位置。我该怎么做?。您的意思是应用程序等待方法 onLocationChanged?
    • 位置更改时会触发 onLocationChanged 事件,您会获得位置(称为 arg0),您可以从该对象中提取 lat 和 lng 并根据该对象更新您的新位置 阅读指南 I补充说它可能会很长,但我向你保证,一旦你完成了,你就会得到你需要的所有答案。
    • 我阅读了该指南,但它并没有满足我的问题的解决方案。我的意思是我不知道如何在 gps 获得良好位置时“休眠”应用程序。
    • 我不明白你想要达到什么目的兄弟......如果你想更新位置,使用 onLocationchanged 基本上可以解决你的问题......
    猜你喜欢
    • 2018-05-26
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多