Skip to content

Instantly share code, notes, and snippets.

@dario61081
Created April 18, 2017 20:06
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 dario61081/3c4a3e758fbc722dd261f292d0a80321 to your computer and use it in GitHub Desktop.
Save dario61081/3c4a3e758fbc722dd261f292d0a80321 to your computer and use it in GitHub Desktop.
Codigo para utilizar GoogleApiClient para el GPS
package py.com.puntofarma.deliverytracker30.services;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
/**
* Created by dario on 22/02/17. Mejoras para el DeliveryTracker20 V1.20
*/
//noinspection
public class GPSServices extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private static final String TAG = "GPSServices";
/**
* The Location listener.
*/
// LocationListener locationListener;
/**
* The Location manager.
*/
// LocationManager locationManager;
// protected static final int REQUEST_CHECK_SETTINGS = 0x1;
private GoogleApiClient googleApiClient = null;
private LocationRequest locationRequest = null;
// private LocationSettingsRequest locationSettingsRequest;
private Location currentLocation;
private static String LOCATION = "location";
@Override
public void onCreate() {
/*configurar el cliente de google*/
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
/*conectar al servicio*/
Log.w(TAG, "onCreate: Conectando el google client");
googleApiClient.connect();
}
@Override
public void onDestroy() {
super.onDestroy();
/*remover las actualizaciones*/
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
/*desconectar del servicio*/
if (googleApiClient != null) {
googleApiClient.disconnect();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.w(TAG, "onConnected: Conectado");
/*conectar a la libreria de google*/
currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (currentLocation != null){
Intent intent = new Intent("servicio_posicion");
intent.putExtra("location", currentLocation);
sendBroadcast(intent);
}
/*iniciar las peticiones*/
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(2000);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
if (i == CAUSE_SERVICE_DISCONNECTED) {
Log.w(TAG, "onConnectionSuspended: Desconectado");;
} else if (i == CAUSE_NETWORK_LOST) {
Log.w(TAG, "onConnectionSuspended: Sin Red" );
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.w(TAG, "onConnectionFailed: Coneccion fallida: " + connectionResult.getErrorCode());
}
@Override
public void onLocationChanged(Location location) {
Log.w(TAG, "onLocationChanged: " + location.toString() );
currentLocation = location;
Intent intent = new Intent("servicio_posicion");
intent.putExtra("location", currentLocation);
sendBroadcast(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment