如何检测启用GPS

时间:2014-06-23 16:51:56

标签: android gps location

我正在制作一款使用GPS的应用。当你在没有启用GPS的情况下打开应用程序时出现问题,它没有做任何事情(显然),但是如果你在没有退出应用程序的情况下启用GPS,那么应用程序仍然无所事事。由于我需要在我的应用中禁用和启用GPS,我需要知道问题的来源,我会在这里留下一些代码:

public class EscuchaLocalizacion implements LocationListener {

    private LocationManager gestor;
    private TextView view1;
    private TextView view2;

    private boolean registrado;
    private static final float minDistance = 10.0f;
    private static final long minTime = 300;

    public double _LONG;
    public double _LAT;

    EscuchaLocalizacion (Context c, TextView t1, TextView t2) {

        Log.v("EscuchaLocalizacion", "Construyendo el objeto");

        gestor = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
        registrado = false;

        view1 = t1;
        view2 = t2;

        Log.v("EscuchaLocalizacion", "He terminado de construir el objeto");

    }


    @Override
    public void onLocationChanged(Location arg0) {
        // TODO Auto-generated method stub

        _LONG = arg0.getLongitude();
        _LAT = arg0.getLatitude();
        view1.setText(Double.toString(_LONG));
        view2.setText(Double.toString(_LAT));
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub
        if(arg0.contentEquals(LocationManager.GPS_PROVIDER)){
            Log.w("EscucharLocalizacion","Provider: " 
                    + LocationManager.GPS_PROVIDER + " was disabled");

        }

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub
        if(!registrado && arg0.contentEquals(LocationManager.GPS_PROVIDER)){
            registrar();
        }


    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

    public void registrar() {

        if (gestor.isProviderEnabled(LocationManager.GPS_PROVIDER)){

            Log.i("EscucharLocalizacion", "Registrado a " + LocationManager.GPS_PROVIDER);
            gestor.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, 
                                        minDistance, this);
            registrado = true;

        }else {

            Log.w("EscucharLocalizacion", "No se pudo registrar a " + LocationManager.GPS_PROVIDER);
            registrado = false;
        }
    }

    public void anularRegistro(){

        gestor.removeUpdates(EscuchaLocalizacion.this);
        registrado = false;
    }

}

1 个答案:

答案 0 :(得分:0)

你在哪里初始化EscuchaLocalizacion?

您需要在onResume()中搜索位置,然后停止在您的活动中搜索暂停位置。

开始定位: escuchaLocalizacion.requestLocationUpdates(LocationManager.GPS_PROVIDER,00,0,gpsLocationListener);

停止定位: escuchaLocalizacion.removeUpdates(gpsLocationListener);

如果您有一个启用/禁用GPS定位的复选框,您可以在复选框上添加一个监听器,然后将此功能放在那里,如果您想使用GPS。

另外不要忘记在清单中添加:

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