1. java
  2. android
  3. c#
  4. .net
  5. javascript
  6. php
  7. jquery
  8. html
  9. sql

Problema com GPS + Android

Galera,

A minha aplicação Android necessitado uso continuo do GPS. Pois bem, esta funcionando, mas depois de um tempo trava a a aplicação. Abaixo os meus códigos

Classe que busca os dados GPS, essa classe eu achei ela na internet.

public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 2000;//1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;

                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }

                }
            }

            // First get location from Network Provider
            if (isNetworkEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }

        } catch (Exception e) {
            Log.e("getLocation", "getLocation", e);
            LogSistema.logError("getLocation", e);
        }

        return location;
    }

    /**
     * Stop using GPS listener Calling this function will stop using GPS in your
     * app
     * */
    public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(GPSTracker.this);
        }
    }

    /**
     * Function to get latitude
     * */
    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * 
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

Método dentro da minha Activity:

    private void buscardadosDoGps() {
        try {
            scheduleTaskExecutorAtualizarStatusDoGPS.scheduleAtFixedRate(new Runnable() {
                public void run() {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                verificarStatusDoSistema();
                                 GPSTracker gps = new
                                 GPSTracker(PrincipalActivity.this);
                                 if (gps.canGetLocation()) {
                                //GerenciaGps gps = GerenciaGps.getGerenciaGps(PrincipalActivity.this);

                                //if (gps.getLatitude() != 0 && gps.getLongitude() != 0) {
                                    double latitude = gps.getLatitude();
                                    double longitude = gps.getLongitude();
                                    // Adicionar a posicao atual do Veiculo
                                    if (mapa != null) {

                                        //ATUALIZA POSIÇÃO NO MAPA
                                    }
                                }
                            }
                        });
                    } catch (Exception e) {
                        Log.e(OperacionalActivity.class.getName(), "atualizarStatusDosEventos", e);
                    }
                }
            }, 0, 1, TimeUnit.SECONDS);
        } catch (Exception e) {
            Log.w(PrincipalActivity.class.getName(), e);
        }
    }

Saída no log

08-14 14:33:47.385: I/LocationManagerService(563): remove 44075868
08-14 14:33:47.416: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.416: I/LocationManagerService(563): remove 428991c8
08-14 14:33:47.440: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.440: I/LocationManagerService(563): remove 43412430
08-14 14:33:47.471: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.471: I/LocationManagerService(563): remove 424affe8
08-14 14:33:47.495: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.495: I/LocationManagerService(563): remove 42c3dea0
08-14 14:33:47.518: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.518: I/LocationManagerService(563): remove 41abe5c0
08-14 14:33:47.549: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.549: I/LocationManagerService(563): remove 4203eb20
08-14 14:33:47.573: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.573: I/LocationManagerService(563): remove 41ad7110
08-14 14:33:47.604: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.604: I/LocationManagerService(563): remove 41847038
08-14 14:33:47.627: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.627: I/LocationManagerService(563): remove 432ab460
08-14 14:33:47.651: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.651: I/LocationManagerService(563): remove 41d6bd08
08-14 14:33:47.682: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.682: I/LocationManagerService(563): remove 41a43408
08-14 14:33:47.705: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.705: I/LocationManagerService(563): remove 41c5b440
08-14 14:33:47.729: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.729: I/LocationManagerService(563): remove 42344a10
08-14 14:33:47.760: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.760: I/LocationManagerService(563): remove 42b2c2e0
08-14 14:33:47.791: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.799: I/LocationManagerService(563): remove 440fc630
08-14 14:33:47.823: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.823: I/LocationManagerService(563): remove 422ba1a0
08-14 14:33:47.870: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.870: I/LocationManagerService(563): remove 438406f0
08-14 14:33:47.916: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.924: I/LocationManagerService(563): remove 41993160
08-14 14:33:47.955: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.955: I/LocationManagerService(563): remove 43d8f700
08-14 14:33:47.979: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:47.979: I/LocationManagerService(563): remove 43a46938
08-14 14:33:48.010: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.010: I/LocationManagerService(563): remove 4365d3a0
08-14 14:33:48.034: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.034: I/LocationManagerService(563): remove 433613e8
08-14 14:33:48.065: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.065: I/LocationManagerService(563): remove 42f21db0
08-14 14:33:48.088: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.088: I/LocationManagerService(563): remove 42af9690
08-14 14:33:48.112: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.112: I/LocationManagerService(563): remove 42935248
08-14 14:33:48.143: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.143: I/LocationManagerService(563): remove 428afb10
08-14 14:33:48.166: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.166: I/LocationManagerService(563): remove 4270b5e8
08-14 14:33:48.190: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.198: I/LocationManagerService(563): remove 426b2480
08-14 14:33:48.221: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.221: I/LocationManagerService(563): remove 4256ad28
08-14 14:33:48.245: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.245: I/LocationManagerService(563): remove 425077b0
08-14 14:33:48.276: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]
08-14 14:33:48.276: I/LocationManagerService(563): remove 42431070
08-14 14:33:48.299: D/LocationManagerService(563): provider request: gps ProviderRequest[ON interval=+2s0ms]

Esta certo esse jeito de buscar os dados do GPS do Android?

Se não qual o caminho que devo tomar?

Att

Vinicius

  1. Você vai ver essas setas em qualquer página de pergunta. Com elas, você pode dizer se uma pergunta ou uma resposta foram relevantes ou não.
  2. Edite sua pergunta ou resposta caso queira alterar ou adicionar detalhes.
  3. Caso haja alguma dúvida sobre a pergunta, adicione um comentário. O espaço de respostas deve ser utilizado apenas para responder a pergunta.
  4. Se o autor da pergunta marcar uma resposta como solucionada, esta marca aparecerá.
  5. Clique aqui para mais detalhes sobre o funcionamento do GUJ!

3 respostas

Não é a resposta que estava procurando? Procure outras perguntas com as tags android java ou faça a sua própria pergunta.