Android Open Source - trip-chain-android Location Receiver






From Project

Back to project page trip-chain-android.

License

The source code is released under:

MIT License

If you think the Android project trip-chain-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fi.aalto.tripchain.receivers;
/*w ww.  j  a  v  a 2 s  .  c om*/
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationRequest;


/**
 * Subscribes to Play Services location updates. 
 * Calls onLocationChanged() when location is received.
 *
 */
public abstract class LocationReceiver implements 
    GooglePlayServicesClient.ConnectionCallbacks, 
    GooglePlayServicesClient.OnConnectionFailedListener,
    com.google.android.gms.location.LocationListener,
    Receiver {
  private final static String TAG = LocationReceiver.class.getSimpleName();
  
  private LocationClient locationClient;
  
  private LocationRequest locationRequest;
  
  protected Context context;
  
  public LocationReceiver(Context context) {
    this.context = context;
    
        locationRequest = LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
        .setInterval(5000)
        .setFastestInterval(1000)
            .setSmallestDisplacement(10);
    
    locationClient = new LocationClient(context, this, this);
  }

  @Override
  public void onConnectionFailed(ConnectionResult result) {
    Log.d(TAG, "Connection failed");
  }

  @Override
  public void onConnected(Bundle connectionHint) {
    Log.d(TAG, "Connection succeeded");

      locationClient.requestLocationUpdates(locationRequest, this);
      Log.d(TAG, "Requested location updates");
  }

  @Override
  public void onDisconnected() {
    Log.i(TAG, "Disconnected");
  }
  
  public void stop() {
    locationClient.removeLocationUpdates(this);
    locationClient.disconnect();
  }
  
  public void start() {
    locationClient.connect();    
  }

  @Override
  public abstract void onLocationChanged(Location location);
}




Java Source Code List

fi.aalto.tripchain.BackgroundService.java
fi.aalto.tripchain.Configuration.java
fi.aalto.tripchain.LoginActivity.java
fi.aalto.tripchain.MainActivity.java
fi.aalto.tripchain.StartFragment.java
fi.aalto.tripchain.TripFragment.java
fi.aalto.tripchain.receivers.ActivityReceiver.java
fi.aalto.tripchain.receivers.EventDispatcher.java
fi.aalto.tripchain.receivers.EventListener.java
fi.aalto.tripchain.receivers.LocationReceiver.java
fi.aalto.tripchain.receivers.Receiver.java
fi.aalto.tripchain.route.ActivityListener.java
fi.aalto.tripchain.route.Activity.java
fi.aalto.tripchain.route.LocationListener.java
fi.aalto.tripchain.route.RoadSegment.java
fi.aalto.tripchain.route.Roads.java
fi.aalto.tripchain.route.RouteSegment.java
fi.aalto.tripchain.route.Route.java
fi.aalto.tripchain.route.TripRecorder.java
fi.aalto.tripchain.route.Trip.java