I am developing a location based application where I have written a service that requests frequent location updates using LocationClient. The application is showing correct current location when my data pack is on but as soon as I switch to WiFi, the application is showing wrong location(to be more precise one of my old locations).
I started the application with data pack on(no WiFi) while leaving for my office. It was showing my correct location till I reached office. After reaching office, I switched to WiFi. When I switched to WiFi, it started showing my home as my current location inspite of me being in office. Switching to data pack again showed my correct location.
Has anyone faced similar issues with LocationClient? I have been struggling with this issue since past two days but could not find any solution or reason for such weird behaviour of LocationClient. Any help would really be appreciated.
Edit My Location Service
public class LocationService extends Service implements LocationListener,
ConnectionCallbacks, OnConnectionFailedListener {
private LocationClient mLocClient;
private LocationRequest mLocRequest;
private IBinder mLocationServiceBinder = new LocationServiceBinder();
private Location mLocation;
private boolean mInProgress = false;
private final static int INTERVAL = 1 * 60 * 1000;
private final static int FASTEST_INTERVAL = 30 * 1000;
private final static int MINIMUM_DISTANCE = 100;
@Override
public void onCreate() {
super.onCreate();
mInProgress = false;
mLocRequest = LocationRequest.create();
mLocRequest.setInterval(INTERVAL);
mLocRequest.setFastestInterval(FASTEST_INTERVAL);
mLocRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocRequest.setSmallestDisplacement(MINIMUM_DISTANCE);
mLocClient = new LocationClient(this, this, this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Service started",
Toast.LENGTH_SHORT).show();
setUpLocationClientIfNeeded();
if (!mLocClient.isConnected() || !mLocClient.isConnecting()
&& !mInProgress) {
mInProgress = true;
mLocClient.connect();
}
return Service.START_STICKY;
}
private void setUpLocationClientIfNeeded() {
if (mLocClient == null)
mLocClient = new LocationClient(this, this, this);
}
@Override
public IBinder onBind(Intent intent) {
return mLocationServiceBinder;
}
@Override
public void onDestroy() {
mInProgress = false;
if(mLocClient != null && mLocClient.isConnected()) {
mLocClient.removeLocationUpdates(this);
mLocClient.disconnect();
}
super.onDestroy();
}
@Override
public void onLocationChanged(Location location) {
//sending a broadcast to activity on location changed
}
// Create a binder class for communication with activity
public class LocationServiceBinder extends Binder {
public LocationService getService() {
return LocationService.this;
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
mInProgress = false;
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i("Location Client", "Connected");
mLocClient.requestLocationUpdates(mLocRequest, this);
mLocation = mLocClient.getLastLocation();
queryParseForDealsAroundLocation(mLocation);
}
@Override
public void onDisconnected() {
mInProgress = false;
mLocClient = null;
Toast.makeText(getApplicationContext(), "Location Client disconnected",
Toast.LENGTH_SHORT).show();
Log.i("Location Client : ", "Disconnected");
}
}