Skip to content

Instantly share code, notes, and snippets.

@tassioauad
Last active January 1, 2016 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tassioauad/8108727 to your computer and use it in GitHub Desktop.
Save tassioauad/8108727 to your computer and use it in GitHub Desktop.
Classe para teste de GPS no Android
package com.auad.gps.service;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class GPSService implements LocationListener{
private Context context;
private LocationManager locationManager;
private Location location;
private static final int DINSTANCIA_MINIMA = 0;
private static final int TEMPO_MINIMO = 0;
public GPSService(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
//Verificando se o GPS está ligado
boolean isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//Verificando se o GPS está ligado
boolean isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnable && !isNetworkEnable) {
throw new RuntimeException("Favor ligar GPS ou prover uma conexão com a internet");
} else {
if(isNetworkEnable) {
//Transformando essa classe em um listener de eventor relacionados ao GPS
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TEMPO_MINIMO, DINSTANCIA_MINIMA, this);
//Adquirindo o último location capturado pelo gps
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(isGPSEnable) {
//Transformando essa classe em um listener de eventor relacionados ao GPS
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEMPO_MINIMO, DINSTANCIA_MINIMA, this);
//Adquirindo o último location capturado pelo gps
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
/**
* Quando o GPS capturar uma nova localização, esse método será executado e receberá
* como parâmetro esse objeto location capturado.
*/
@Override
public void onLocationChanged(Location location) {
Log.i("GPS-"+location.getProvider(), location.getLatitude() + ", " + location.getLongitude());
this.location = location;
}
/**
* Esse método será chamado quando o GPS for desligado pelo usuário.
*/
@Override
public void onProviderDisabled(String provider) {
if(provider.equals(LocationManager.GPS_PROVIDER)) {
Toast.makeText(context, "Você desabilitou o seu GPS!", Toast.LENGTH_LONG).show();
} else if(provider.equals(LocationManager.NETWORK_PROVIDER)) {
Toast.makeText(context, "Você desabilitou a sua internet!", Toast.LENGTH_LONG).show();
}
}
/**
* Esse método será chamado quando o GPS for ligado pelo usuário.
*/
@Override
public void onProviderEnabled(String provider) {
if(provider.equals(LocationManager.GPS_PROVIDER)) {
Toast.makeText(context, "Você habilitou o seu GPS!", Toast.LENGTH_LONG).show();
} else if(provider.equals(LocationManager.NETWORK_PROVIDER)) {
Toast.makeText(context, "Você habilitou a sua internet!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public double getLatitude() {
return location.getLatitude();
}
public double getLongitude() {
return location.getLongitude();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment