How to get gps location once for 5 mins android?

前端 未结 4 1832
独厮守ぢ
独厮守ぢ 2021-02-01 10:55

hi there can anybody give me a sample code for get location for every five minutes please i have tried and i can get location once by cliking on button, but i need it to be disp

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 11:39

    Hi Use the below timer code.

    You can use the below options option 1 this will get the locations if mobile moved 100meters.

        captureFrequencey=3*60*1000;   
    LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, captureFrequencey, 100, this);
    

    have a look at this link http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29

    Option 2

       TimerTask refresher;
            // Initialization code in onCreate or similar:
            timer = new Timer();    
            refresher = new TimerTask() {
                public void run() {
                  handler.sendEmptyMessage(0);
                };
            };
            // first event immediately,  following after 1 seconds each
            timer.scheduleAtFixedRate(refresher, 0,1000); 
            //=======================================================
    
    
    final Handler handler = new Handler() {
    
    
            public void handleMessage(Message msg) {
                  switch (msg.what) {
                  case REFRESH: 
                      //your code here 
    
                      break;
                  default:
                      break;
                  }
              }
            };
    

    Timer will call the handler for your time duration (change 1000 into your required time ).

    Hope this will help you.

提交回复
热议问题