問題描述
我正在考慮設(shè)置兩個(gè)單獨(dú)的警報(bào)來每小時(shí)收集用戶的位置數(shù)據(jù),一個(gè)每 59 分鐘觸發(fā)一次以連接"客戶端,另一個(gè)用于實(shí)際獲取位置,然后斷開客戶端.
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.
在電池壽命方面,如果獲取用戶的位置將成為應(yīng)用程序的主要消耗,我還應(yīng)該考慮做些什么?或者,是否有不同的方法來設(shè)置兩個(gè)警報(bào)?我最初只有一個(gè)警報(bào),但執(zhí)行 (!mLocationClient.isConnected) 然后進(jìn)行連接檢查并沒有給客戶端足夠的時(shí)間來連接.
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.
感謝您的洞察力.
兩個(gè)警報(bào)會(huì)像這樣響起:
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?
如果只是關(guān)于 UI,則使用 getLastKnownLocation(PASSIVE_PROVIDER),假設(shè)他們在其他地方使用手機(jī)上的定位服務(wù),您應(yīng)該會(huì)得到一些半準(zhǔn)確的信息.
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.
如果您需要實(shí)際對(duì)用戶進(jìn)行三角測量,請實(shí)現(xiàn)不同的供應(yīng)商使用不同的電池.被動(dòng)<網(wǎng)絡(luò)<全球定位系統(tǒng).
If you need to actually triangulate the user, realize the different providers use different battery. Passive < Network < GPS.
定位用戶越多,GPS 消耗的電量和時(shí)間就越多.
The more you locate the user, the more battery with GPS taking the most battery and time.
按計(jì)劃啟動(dòng)服務(wù),1 小時(shí)或其他任何時(shí)間,只需要一項(xiàng)服務(wù).最多只能活 1 分鐘(或更短),收聽所有位置提供商.在分鐘或準(zhǔn)確度足夠好后,您保存結(jié)果并關(guān)閉服務(wù).
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.
這篇關(guān)于使用 LocationClient 定期獲取更新最省電的方法是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!