2017-01-10 32 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

相關問題