Created
February 27, 2017 19:54
-
-
Save shakil807g/12078524462e73523b62fb3dd25dafa1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { | |
private LocationRequest mLocationRequest; | |
private GoogleApiClient mGoogleApiClient; | |
private static final String LOGSERVICE = "#######"; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
buildGoogleApiClient(); | |
Log.i(LOGSERVICE, "onCreate"); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.i(LOGSERVICE, "onStartCommand"); | |
if (!mGoogleApiClient.isConnected()) | |
mGoogleApiClient.connect(); | |
return START_STICKY; | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
Log.i(LOGSERVICE, "onConnected" + bundle); | |
Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); | |
if (l != null) { | |
Log.i(LOGSERVICE, "lat " + l.getLatitude()); | |
Log.i(LOGSERVICE, "lng " + l.getLongitude()); | |
} | |
startLocationUpdate(); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
Log.i(LOGSERVICE, "onConnectionSuspended " + i); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.i(LOGSERVICE, "lat " + location.getLatitude()); | |
Log.i(LOGSERVICE, "lng " + location.getLongitude()); | |
LatLng mLocation = (new LatLng(location.getLatitude(), location.getLongitude())); | |
EventBus.getDefault().post(mLocation); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Log.i(LOGSERVICE, "onDestroy - Estou sendo destruido "); | |
} | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
Log.i(LOGSERVICE, "onConnectionFailed "); | |
} | |
private void initLocationRequest() { | |
mLocationRequest = new LocationRequest(); | |
mLocationRequest.setInterval(5000); | |
mLocationRequest.setFastestInterval(2000); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
} | |
private void startLocationUpdate() { | |
initLocationRequest(); | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
// TODO: Consider calling | |
// ActivityCompat#requestPermissions | |
// here to request the missing permissions, and then overriding | |
// public void onRequestPermissionsResult(int requestCode, String[] permissions, | |
// int[] grantResults) | |
// to handle the case where the user grants the permission. See the documentation | |
// for ActivityCompat#requestPermissions for more details. | |
return; | |
} | |
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); | |
} | |
private void stopLocationUpdate() { | |
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); | |
} | |
protected synchronized void buildGoogleApiClient() { | |
mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addOnConnectionFailedListener(this) | |
.addConnectionCallbacks(this) | |
.addApi(LocationServices.API) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment