2011-09-21 90 views
1

我是android新手。我將使用下面的代碼獲取android設備的緯度和經度,但我希望每15分鐘更新一次緯度和經度。我怎麼能得到這個? 請幫幫我。如何每15分鐘更新一次地面經度?

在此先感謝。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    latituteField = (TextView) findViewById(R.id.TextView02); 
    longitudeField = (TextView) findViewById(R.id.TextView04); 

    // Get the location manager 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    // Define the criteria how to select the locatioin provider -> use 
    // default 
    Criteria criteria = new Criteria(); 
    provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 

    // Initialize the location fields 
    if (location != null) { 
     System.out.println("Provider " + provider + " has been selected."); 
     int lat = (int) (location.getLatitude()); 
     int lng = (int) (location.getLongitude()); 
     latituteField.setText(String.valueOf(lat)); 
     longitudeField.setText(String.valueOf(lng)); 
    } else { 
     latituteField.setText("Provider not available"); 
     longitudeField.setText("Provider not available"); 
    } 
} 

/* Request updates at startup */ 
@Override 
protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(provider, 400, 1, this); 
} 

/* Remove the locationlistener updates when Activity is paused */ 
@Override 
protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(this); 
} 

@Override 
public void onLocationChanged(Location location) { 
    int lat = (int) (location.getLatitude()); 
    int lng = (int) (location.getLongitude()); 
    latituteField.setText(String.valueOf(lat)); 
    longitudeField.setText(String.valueOf(lng)); 
} 

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

} 

@Override 
public void onProviderEnabled(String provider) { 
    Toast.makeText(this, "Enabled new provider " + provider, 
      Toast.LENGTH_SHORT).show(); 

} 

@Override 
public void onProviderDisabled(String provider) { 
    Toast.makeText(this, "Disenabled provider " + provider, 
      Toast.LENGTH_SHORT).show(); 
} 
} 

+0

可能的重複[我如何發送緯度經度的Android設備每15分鐘到PHP Web服務](http://stackoverflow.com/questions/7440470/how-can-i-send-latitude-longitude -of-機器人裝置-每-15分鐘到PHP-web的SER/7440507#7440507)。 – user370305

回答

3

OK,

!. Start a service or thread from your activity. 

2. put a timer for every 15 minutes. 

3. at every 15 minutes just call the onLocationChanged() for location update and 
    save it then pass the value to your activity and use it. 

現在我覺得這個時候你瞭解它。

編輯:也位置更新只是檢查前面的問題,因爲我在上面評論中提到。

謝謝。 :-)