問題描述
我有一個安卓應用程序.根據用戶當前的地理位置,我想在后臺獲取一些遠程數據并存儲.我的實現是:
I have an android application. Based on the current geo location of user, I want to fetch some remote data in background and store it. My implementation is:
在特定的時間間隔,警報會啟動我的服務.服務使用匿名類查詢當前位置并注冊一個 locationListener 回調.在調用 onLocationChanged() 時,我啟動從服務器獲取遠程數據.
At specific interval a alarm fires up my service. Service uses an anonymous class to query current location and registers a locationListener callback. On call of onLocationChanged() I initiate the remote data fetch from server.
但是,一旦我的服務完成使用匿名類注冊位置偵聽器,它就會按預期返回;因為它不會在完成之前等待回調發生.由于回調需要一些時間并在服務已經返回時進行調用,所以它會拋出一個錯誤:
However once my service is done registering the location listener using anonymos class, it returns as expected; as it doesn't wait for callback to happen before finishing. Since callback takes some time and makes a call when service has already returned, it throws an error saying:
java.lang.RuntimeException: Handler{43e82510} sending message to a Handler on a dead thread
這很好理解.現在對我來說一個快速的解決方法是我可以使用 locationManager 中的 getLastKnownLocation ,因為它不會通過回調響應;但是如果我現在確實想要最新的位置,在服務中而不是活動中怎么辦?如何等待回調發生并停止我的服務返回.
Which is quite understandable. One quick workaround for me now is that I can use getLastKnownLocation from locationManager as that doesn't respond back by callback; but what if I do want the latest location right now, in a service and not activity? How can I wait for callback to happen and stop my service from returning.
另外,lastKnownlocation 什么時候更新?每次 GPS 注冊一個新位置時;它會更新嗎?我想知道的是,如果它不是最新的,它仍然可以關閉到最新嗎?因為我在 android 模擬器中沒有看到配置后續更新之間的時間段的選項.
Also, at what point does lastKnownlocation gets updated? Everytime GPS registers a new location; does it update it? What I want to know is that if it's not latest can it still be closed to latest? As I didn't see an option in android emulator to configure the time period between subsequent updates.
非常感謝任何幫助.干杯
Any help is much appreciated. Cheers
推薦答案
但是如果我現在確實想要最新的位置,在服務中而不是在活動中呢?
but what if I do want the latest location right now, in a service and not activity?
抱歉,這在服務或活動中都是不可能的.例如,如果 GPS 無線電已關閉,而您正在從 GPS 請求位置數據,則需要數十秒才能得到修復,如果您很幸運的話.它可能根本無法修復.
Sorry, but that is not possible, in either a service or an activity. For example, if the GPS radio is off, and you are requesting location data from GPS, it will take tens of seconds just to get a fix, and that's if you are lucky. It might not get a fix at all.
如何等待回調發生并阻止我的服務返回.
How can I wait for callback to happen and stop my service from returning.
你沒有.你按照你說的去做:
You don't. You do what you said you would do:
使用 locationManager 中的 getLastKnownLocation,因為它不會通過回調響應
use getLastKnownLocation from locationManager as that doesn't respond back by callback
所以,讓您的 Service
(希望是 IntentService
)檢查 getLastKnownLocation()
是否恰好有一個值.如果是,請使用它.否則,registerLocationUpdates()
使用 PendingIntent
將控制權交還給您的 IntentService
.當你得到那個 Intent
時,使用該位置并取消注冊更新(假設警報周期很長,比如每小時一次).
So, have your Service
(which is hopefully an IntentService
) check to see if getLastKnownLocation()
happens to have a value. If it does, use it. Otherwise, registerLocationUpdates()
using a PendingIntent
that will pass control back to your IntentService
. When you get that Intent
, use the location and unregister for updates (assuming the alarm period is nice and long, like, say, once an hour).
如果您的鬧鐘是 _WAKEUP 鬧鐘,事情就會變得棘手.然后,您需要按住 WakeLock
,以便在您嘗試 GPS 定位時設備不會重新進入睡眠狀態.但是,您需要在某個時候釋放該 WakeLock
,如果我們無法獲得 GPS 定位...嗯...嗯,這就是棘手的部分.試圖找出一種干凈的方式來處理這個問題,并將其實現為可重用的組件(例如,LocationAlarmService
),這是我的待辦事項列表中的 18,000 項之一.
Things get tricky if your alarm is a _WAKEUP alarm. You will then need to hold a WakeLock
, so the device does not fall back asleep while you are trying to get a GPS fix. However, you need to release that WakeLock
sometime, and if we cannot get a GPS fix...ummm...well, that's the tricky part. Trying to figure out a nice clean way of handling this, and implementing it as a reusable component (e.g., LocationAlarmService
), is one of 18,000 items on my to-do list.
另外,lastKnownlocation 什么時候更新?每次 GPS 注冊一個新位置時;它 > 更新了嗎?
Also, at what point does lastKnownlocation gets updated? Everytime GPS registers a new location; does it > update it?
AFAIK,是的.
這篇關于帶有 locationListener 回調的 Android 服務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!