問題描述
我的應用會在特定時間檢查用戶是否在給定位置.我使用警報管理器來啟動進行此調用的服務:
My app checks at a specific time whether a user is at a given location. I use the alarm manager to start a service that makes this call:
locationManager.requestLocationUpdates(bestProvider, 0, 0, listener);
并且還檢查:
locationManager.getLastKnownLocation(bestProvider);
但我在真實設備上運行時遇到問題.一方面,getLastKnownLocation
很可能是 GPS 的最后一個位置,它可能在任何地方(即,它可能距離用戶當前位置數英里).所以我會等待 requestLocationUpdates
回調,如果它們在兩分鐘內不存在,請移除偵聽器并放棄,對嗎?
But I'm having problems when running on a real device. For one thing, getLastKnownLocation
is most likely the last place the GPS was on, which could be anywhere (i.e., it could be miles from the user's current location). So I'll just wait for requestLocationUpdates
callbacks, and if they aren't there within two minutes, remove the listener and give up, right?
錯了,因為如果用戶的位置已經穩定(即他們最近使用了 GPS 并且沒有移動),那么我的監聽器將永遠不會被調用,因為位置不會改變.但是 GPS 會一直運行,直到我的監聽器被移除,耗盡電池...
Wrong, because if the user's location is already stable (i.e., they've used GPS recently and haven't moved) then my listener will never be called because the location doesn't change. But the GPS will run until my listener is removed, draining the battery...
在不將舊位置誤認為當前位置的情況下,獲取當前位置的正確方法是什么?我不介意等幾分鐘.
What is the right way to get the current location without mistaking an old location for the current location? I don't mind waiting a few minutes.
我可能錯了沒有調用聽眾,這可能比我想象的要長一點......很難說.我仍然希望得到一個明確的答案.
It's possible that I'm wrong about the listener not being called, it may just take a little longer than I thought... Hard to say. I'd appreciate a definitive answer still.
推薦答案
代碼可能是這樣的:
public class MyLocation {
Timer timer1;
LocationManager lm;
public boolean getLocation(Context context)
{
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
lm.removeUpdates(this);
//use location as it is the latest value
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//use location as we have not received the new value from listener
}
}
}
我們啟動監聽器并等待更新一段時間(在我的示例中為 20 秒).如果我們在此期間收到更新,我們會使用它.如果在此期間我們沒有收到更新,我們會使用 getLastKnownLocation 值并停止監聽器.
We start the listener and wait for update for some time (20 seconds in my example). If we receive update during this time we use it. If we don't receive an update during this time we use getLastKnownLocation value and stop the listener.
您可以在此處查看我的完整代碼 在 Android 上獲取用戶當前位置的最簡單、最可靠的方法是什么?
You can see my complete code here What is the simplest and most robust way to get the user's current location on Android?
編輯(由提問者):這是大部分答案,但我的最終解決方案使用 Handler 而不是 Timer.
EDIT (by asker): This is most of the answer, but my final solution uses a Handler instead of a Timer.
這篇關于Android - 可靠地獲取當前位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!