我需要跟踪应用用户在特定位置花费的时间。请建议我一个优化的方法。我查看了这个link。
我可以使用上面的方法来获取位置,但是我的方法应该达到什么目的?
答案 0 :(得分:0)
主要想法是以定义的时间间隔获取当前用户位置,如果位置超出您的区域,我们可以计算时差
按照以下步骤
long time1 = new Date().getTime();
long time2;
double firstLat, firstLog;
// fetch current user location using this link
// https://developer.android.com/training/location/retrieve-current.html
//Declare the timer
Timer timer = new Timer();
//Set the schedule function and rate
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Called each time when 1000 milliseconds (1 second) (the period parameter)
double curLat, curLog;
// fetch current user location using this link
// https://developer.android.com/training/location/retrieve-current.html
if(!isUserInRegion(firstLat,firstLog,curLat,curLog){
time2 = new Date().getTime();
timer.cancel();
timer.purge();
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
long timeDiff = time2 - time1;
private boolean isUserInRegion(double firstLat, double firstLog, double curLat, double curLog){
int r = 0.005; // Any radious
double dx = curLat - firstLat;
double dy = curLog - firstLog;
return dx * dx + dy * dy <= r * r;
}
希望这个技巧对你有用:)