2017-01-10 4 views
-1

特定の場所でアプリユーザーが費やした時間を追跡する必要があります。私に最適なアプローチを提案してください。私はこれを調べたlinkGoogleの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

関連する問題