Ted pottel
Ted pottel

Reputation: 6983

Is there a way to get GPS location on a time interval instead of when the position changes?

My client wants a GPS app that will produce a spreadsheet of all the GPS locations in equal time intervals. You can set the time interval using a spinner.

Right now my app gets the new GPS location when the location changes using LocationManager manager's onLocationChanged(Location loc) callback.

Is there a way to get the new GPS locations on time intervals instead of when the position changes?

Code:

public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  


  //if you want to lock screen for always Portrait mode    
  setRequestedOrientation(ActivityInfo  
  .SCREEN_ORIENTATION_PORTRAIT);  

  pb = (ProgressBar) findViewById(R.id.progressBar1);  
  pb.setVisibility(View.INVISIBLE);  

  viewLog= (TextView) findViewById(R.id.ViewLong); 
  viewLat= (TextView) findViewById(R.id.ViewLat); 

  locationMangaer = (LocationManager)   
  getSystemService(Context.LOCATION_SERVICE);  
  StartGPS();
 }  


void  StartGPS()
{

 flag = displayGpsStatus();  
  if (flag) {  


   pb.setVisibility(View.VISIBLE);  
   locationListener = new MyLocationListener();  

   locationMangaer.requestLocationUpdates(LocationManager .GPS_PROVIDER, 5000, 10,locationListener);  

   } else {  
   alertbox("Gps Status!!", "Your GPS is: OFF");  
  }  

 }  




@Override  
 public void onClick(View v) {  

 }  



 /*----------Listener class to get coordinates ------------- */  
 private class MyLocationListener implements LocationListener {  
        @Override  
        public void onLocationChanged(Location loc) {  

            String longitude = "Longitude: " +loc.getLongitude(); 
               viewLog.setText(longitude);  
               String latitude = "Latitude: " +loc.getLatitude(); 
               viewLat.setText( latitude); 
               pb.setVisibility(View.INVISIBLE); 

        }  

        @Override  
        public void onProviderDisabled(String provider) {  
            // TODO Auto-generated method stub           
        }  

        @Override  
        public void onProviderEnabled(String provider) {  
            // TODO Auto-generated method stub           
        }  

        @Override  
        public void onStatusChanged(String provider,   
  int status, Bundle extras) {  
            // TODO Auto-generated method stub           
        }  
    }  

Upvotes: 0

Views: 790

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

Register for location updates as you are now. Ignore the values passed to your LocationListener. On your desired polling frequency, call getLastKnownLocation() for whatever provider you are using for your location updates.

Upvotes: 1

Related Questions