Reputation: 2239
I am trying to update the location from service but when I call googleApiClient to connect it is not calling any of the methods such as onConnected, onConnectionSuspended or onConnectionFailed.
Here's the full code :
public abstract class BASE_LOCAT_SERVICE extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
public String TAG="LOC_SERVICE";
public GoogleApiClient googleApiClient;
private Location lastLocation;
private static int UPDATE_INTERVAL = 5000;
private static int DISPLACEMENT = 10;
private LocationRequest locationRequest;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG+"_LOC","On create Command");
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
Log.i(TAG+"_LOC","googleApiClient Connecting");
}
protected void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setSmallestDisplacement(DISPLACEMENT);
Log.i(TAG+"_LOC","createLocationRequest");
}
private void initialiseWithLastLocation() {
try{
lastLocation = LocationServices.FusedLocationApi.getLastLocation(
googleApiClient);
Log.i(TAG+"_LOC","initialiseWithLastLocation Initialised");
}catch(SecurityException ex){
Log.e(TAG+"_LOC", "Unable to get last known location", ex);
}
}
protected void startLocationUpdates() {
try {
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
Log.i(TAG+"_LOC","startLocationUpdates Initialised");
} catch (SecurityException ex) {
Log.e(TAG+"_LOC", "Unable to get Last Known location", ex);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
createLocationRequest();
initialiseWithLastLocation();
startLocationUpdates();
Log.i(TAG+"_LOC","googleApiClient Connected");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG+"_LOC","googleApiClient onConnectionSuspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i(TAG+"_LOC","googleApiClient onConnectionFailed");
}
@Override
public void onLocationChanged(Location location) {
lastLocation = location;
Log.i(TAG+"_LOC"," onLocationChanged");
}
public String getLastLocation()
{
if(lastLocation!=null)
{
String ret="Location : \n";
ret = ret+"Latitude : "+lastLocation.getLatitude()+"\n";
ret = ret+"Longitude : "+lastLocation.getLongitude()+"\n";
ret = ret+"Altitude : "+lastLocation.getAltitude()+"\n";
ret = ret+"Accuracy : "+lastLocation.getAccuracy()+"\n";
ret = ret+"Provider : "+lastLocation.getProvider()+"\n";
ret = ret+"Speed : "+lastLocation.getSpeed()+"\n";
ret = ret+"MAP Link : https://www.google.com/maps/?q="+lastLocation.getLatitude()+","+lastLocation.getLongitude()+"\n";
return ret;
}
else
return "Location Not Available";
}
}
and the logcat
shows only this:
LOC_SERVICE_LOC: On create Command
LOC_SERVICE_LOC: googleApiClient Connecting
Upvotes: 0
Views: 422
Reputation: 6834
You have to connect using mGoogleApiClient.connect() inside onStartCommand
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}else{
Toast.makeText(getApplicationContext(),"mGoogleApiClient is null",Toast.LENGTH_LONG).show();
}
return START_STICKY;
}
Upvotes: 3