问题描述
我正在考虑设置两个单独的警报来每小时收集用户的位置数据,一个每 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 定期获取更新最省电的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!