2017-01-10 68 views
-1

我需要跟踪应用程序用户在特定位置所花费的时间。请给我一个优化的方法。我看着这个link使用Google apis在Android中的位置上追踪时间

我可以使用上面的获取位置,但我的方法来实现我想要的?

+0

你想在特定点或区域的时间? – Piyush

+0

对不起,延迟回复。我特别想要时间。 – Moiz

回答

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; 
} 

希望如此,这一招的作品为你:)

+0

感谢您的回复Dhruv。我希望这会起作用。 – Moiz