How to maintain GPS coordinates obtained between Activities?

Asked

Viewed 116 times

1

I’m trying to get the GPS coordinates on Android.
I developed the search class and I get the position normally but the problem is that the user does not have time to wait for the device to find the position and to navigate and a new Activity is called, I lose the position of latitude and longitude and it is necessary to instantiate again the class Updater on the next screen to find the position again.

How can I solve this problem?
The application is to generate transport order and has eight screens.

I created the class Updater below which is instantiated through the method ativaEBuscaCoordenadaGPS().

package usd.com.br.ordemdetransporte.gps;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

/**
 * Created by marcelo on 23/02/2016.
 */
public class AtualizadorDeLocalizacao implements LocationListener {
private LocationManager manager;
private Context context;
boolean canGetLocation = false;     // flag for GPS status
boolean isGPSEnabled = false;       // flag for GPS status
private double latitude;
private double longitude;
//private OfertaIrresistivelApplication cache;
private Activity activity;

public AtualizadorDeLocalizacao(Context context, Activity activity) {
    this.context = context;
    this.activity = activity;

    //cache = (OfertaIrresistivelApplication) activity.getApplication();

    manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

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

    if (isGPSEnabled) {
        this.canGetLocation = true;
        Toast.makeText(this.context, "GPS habilitado !", Toast.LENGTH_LONG).show();

    } else {
        Toast.makeText(this.context, "GPS Desabilitado !", Toast.LENGTH_LONG).show();
        showSettingsAlert();
    }

    int distanciaMinima = 1000; // mil metros 1000
    int tempoMinimo = 300; // sessenta segundos 60000

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, tempoMinimo, distanciaMinima, this);

}

public void cancela() {
    manager.removeUpdates(this);
}


@Override
public void onLocationChanged(Location location) {

    // check if GPS enabled
    if (canGetLocation()) {

        location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //pegar apenas 1 posicao

        latitude = location.getLatitude();
        longitude = location.getLongitude();


        if (latitude != 0 && longitude != 0) {

            Toast.makeText(this.context, "Posição GPS. Latitude: " + latitude + " longitude: " + longitude, Toast.LENGTH_LONG).show();

            //cache.setLatitude(latitude);
            //cache.setLongitude(longitude);
        }
    } else {
        showSettingsAlert();
    }

}

@Override
public void onProviderDisabled(String provider) {
    //MyLog.i("AtualizadorDeLocalizao Provedor "  + provider + " Disabled" );
}

@Override
public void onProviderEnabled(String provider) {
    //MyLog.i("AtualizadorDeLocalizao Provedor "  + provider + " Enabled" );
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    //MyLog.i("AtualizadorDeLocalizao Provedor "  + provider + " Changed" );
}

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

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 */
public void showSettingsAlert() {
//        AlertDialog.Builder alertDialog = new  AlertDialog.Builder(this.context);
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.activity);

    // Setting Dialog Title
    alertDialog.setTitle("Configurar GPS");

    // Setting Dialog Message
    alertDialog.setMessage("GPS esta desligado. Deseja configura-lo ? ");

    // On pressing Settings button
    alertDialog.setPositiveButton("Configurar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            try {
                context.startActivity(intent);
                //cache.setGPS(true);
            } catch (Exception e) {
                String erro = e.getMessage();
                //MyLog.i("Erro: " + erro);
                Toast.makeText(activity, "Não Possui GPS: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                //cache.setGPS(false);
            }
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();

    // Showing Alert Message
    /*if(cache.getGPS()){
        alertDialog.show();
    }*/
}


public double getLatitude() {
    return latitude;
}

public double getLongitude() {
    return longitude;
}

}



public void ativaEBuscaCoordenadaGPS() {

    if (locationListener == null) {
        locationListener = new AtualizadorDeLocalizacao(app.getApplicationContext(), OrdemTransporte8.this);
    }

    // check if GPS enabled
    if (locationListener.canGetLocation()) {
    } else {
        locationListener.showSettingsAlert();
    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.