我正在开发一个基于位置的应用程序,我在其中编写了一个使用 LocationClient 请求频繁位置更新的服务。当我的数据包打开时,应用程序显示正确的当前位置,但一旦我切换到 WiFi,应用程序显示错误的位置(更准确地说是我的旧位置之一)。
我在去办公室时打开了数据包(无 WiFi)的应用程序。在我到达办公室之前,它显示了我的正确位置。到达办公室后,我切换到WiFi。当我切换到 WiFi 时,它开始将我的家显示为我当前的位置,尽管我在办公室。再次切换到数据包显示了我的正确位置。
有没有人遇到过与 LocationClient 类似的问题?自过去两天以来,我一直在努力解决这个问题,但找不到任何解决方案或原因导致 LocationClient 的这种奇怪行为。任何帮助将不胜感激。
编辑 我的位置服务
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");
}
}