Android: Send user location to the server every 15 minutes only if the location has been changed?

99封情书 提交于 2019-12-06 05:03:45

Yo can use Service class as follows ,

public class StartService extends Service {

Timer timer = new Timer();
private final int TIME_INTERVAL = 10000;
GPSTracker GPSTracker;
Double latitude  ;
Double longitude ;



@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    GPSTracker = new GPSTracker(StartService.this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    doTimerThings();
    return super.onStartCommand(intent, flags, startId);
}

private void doTimerThings() 
{
    timer.scheduleAtFixedRate(new TimerTask() {

        @SuppressLint("DefaultLocale")
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override
        public void run() {



            latitude = GPSTracker.getLatitude();
            longitude = GPSTracker.getLongitude();

            // you get the lat and lng , do your server stuff here-----

            System.out.println("lat------ "+latitude);
            System.out.println("lng-------- "+longitude);
        }

    }, 0, TIME_INTERVAL);

}




@Override
public void onDestroy() {
    super.onDestroy();
}


}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!