2012-10-10 40 views
0

我的客戶想要一個GPS應用程序,它將以相同的時間間隔生成所有GPS位置的電子表格。您可以使用微調器設置時間間隔。有沒有辦法在一定時間間隔內獲取GPS位置,而不是位置發生變化?

現在我的應用獲取新的GPS位置,當位置改變使用LocationManager經理的onLocationChanged(Location loc)回調。

有沒有辦法按時間間隔獲取新的GPS位置,而不是位置變化的時間?

代碼:

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   
     } 
    } 

回答

1

註冊位置更新你現在的樣子。忽略傳遞給您的值LocationListener。在您所需的輪詢頻率上,撥打getLastKnownLocation()即可獲得您用於位置更新的任何提供商。

相關問題