LocationClient を使用して頻繁に位置情報の更新を要求するサービスを作成した位置情報ベースのアプリケーションを開発しています。データ パックがオンの場合、アプリケーションは正しい現在位置を表示しますが、WiFi に切り替えるとすぐに、アプリケーションは間違った位置を表示します (より正確には、以前の位置の 1 つです)。
オフィスに向かう途中、データ パックをオン (WiFi なし) にしてアプリケーションを開始しました。私がオフィスに着くまで、それは私の正しい場所を示していました。オフィスに着いた後、私はWiFiに切り替えました。WiFiに切り替えると、オフィスにいるにも関わらず自宅が現在地として表示されるようになりました。再びデータパックに切り替えると、正しい場所が表示されました。
LocationClient で同様の問題に直面した人はいますか? 過去 2 日間、この問題に取り組んできましたが、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");
}
}