問題描述
我正在考慮設置兩個單獨的警報來每小時收集用戶的位置數據,一個每 59 分鐘觸發一次以連接"客戶端,另一個用于實際獲取位置,然后斷開客戶端.
I am thinking about having two separate alarms to gather a user's location data every hour, one that goes off every 59 minutes to "connect" the client and a second to actually get the location and then subsequently disconnect the client.
在電池壽命方面,如果獲取用戶的位置將成為應用程序的主要消耗,我還應該考慮做些什么?或者,是否有不同的方法來設置兩個警報?我最初只有一個警報,但執行 (!mLocationClient.isConnected) 然后進行連接檢查并沒有給客戶端足夠的時間來連接.
In terms of battery life, is there anything else I should consider doing if getting the user's location will be the primary drain of the app? Or, is there a different approach to having two alarms? I originally only had a single alarm, but performing a (!mLocationClient.isConnected) then connect check does not give the client enough time to connect.
感謝您的洞察力.
兩個警報會像這樣響起:
The two alarms would go off something like this:
private int PERIODIC_UPDATE = 60000*60; //gets location and disconnects every hour
private int PERIODIC_RECONNECTION_UPDATE = 60000*59; //connects 1 minute before getLocation call
Timer toReconnect = new Timer();
toReconnect.schedule(new TimerTask() {
@Override
public void run() {
mLocationClient.connect();
}
}, 5000, PERIODIC_RECONNECTION_UPDATE);
Timer theTimer = new Timer();
theTimer.schedule(new TimerTask(){
@Override
public void run() {
try {
if(!mLocationClient.isConnected()) {
mLocationClient.connect();
//This will not have much affect because cannot so quickly, will remove.
}
Location theLocation = mLocationClient.getLastLocation();
if(theLocation!=null) {
checkPostLocation(theLocation);
mLocationClient.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}}, 5000, PERIODIC_UPDATE);
推薦答案
你真的需要跟蹤用戶嗎?
Do you actually need to track the user?
如果只是關于 UI,則使用 getLastKnownLocation(PASSIVE_PROVIDER),假設他們在其他地方使用手機上的定位服務,您應該會得到一些半準確的信息.
If it's just about UI, then use getLastKnownLocation(PASSIVE_PROVIDER) and you should get something semi-accurate assuming they used location services on their phone somewhere else.
如果您需要實際對用戶進行三角測量,請實現不同的供應商使用不同的電池.被動<網絡<全球定位系統.
If you need to actually triangulate the user, realize the different providers use different battery. Passive < Network < GPS.
定位用戶越多,GPS 消耗的電量和時間就越多.
The more you locate the user, the more battery with GPS taking the most battery and time.
按計劃啟動服務,1 小時或其他任何時間,只需要一項服務.最多只能活 1 分鐘(或更短),收聽所有位置提供商.在分鐘或準確度足夠好后,您保存結果并關閉服務.
Start the service by intent one a schedule, 1 hour or whatever, only one service necessary. Only live for a maximum of 1 minute (or less), listen on all Location providers. After the minute or accuracy is good enough, you save the result and shut down the service.
這篇關于使用 LocationClient 定期獲取更新最省電的方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!