2013-06-05 46 views
4

我正在尋找LocationManager以獲取定期報告位置的最佳位置。我很好奇LocationManager.requestLocationUpdates方法。如果我使用不同的minTime和minDistance多次調用此方法而不調用removeUpdates會發生什麼? 它會添加新的請求還是隻更新現有的請求?
我想測試一下,這一點目前並不容易。您的答覆將不勝感激。LocationManager.requestLocationUpdates添加或更新?

+0

謝謝你們回答我。 – sunghun

回答

5

它會更新現有的請求。

+0

相當古老的問題,但我今天看了:)我們錯過了一些來源! – mithrop

+0

消息來源確實不錯。但是我可以確認這個答案是正確的。 – scai

2

您可以多次註冊。 請參閱下面的代碼。爲Android

package com.test.locationmanager; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.widget.TextView; 

public class LocationManagerStatus extends Activity { 

private LocationManager locationManager; 
private TextView textView; 
private final LocationListener gpsLocationListener =new LocationListener(){ 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     final String tvTxt = textView.getText().toString(); 
     switch (status) { 
     case LocationProvider.AVAILABLE: 
      textView.setText(tvTxt + "GPS available again\n"); 
      break; 
     case LocationProvider.OUT_OF_SERVICE: 
      textView.setText(tvTxt + "GPS out of service\n"); 
      break; 
     case LocationProvider.TEMPORARILY_UNAVAILABLE: 
      textView.setText(tvTxt + "GPS temporarily unavailable\n"); 
      break; 
     } 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "GPS Provider Enabled\n"); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "GPS Provider Disabled\n"); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     locationManager.removeUpdates(networkLocationListener); 
     textView.setText(textView.getText().toString() 
       + "New GPS location: " 
       + String.format("%9.6f", location.getLatitude()) + ", " 
       + String.format("%9.6f", location.getLongitude()) + "\n"); 
    } 
}; 
private final LocationListener networkLocationListener = 
                new LocationListener(){ 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras){ 
     final String tvTxt = textView.getText().toString(); 
     switch (status) { 
     case LocationProvider.AVAILABLE: 
      textView.setText(tvTxt + "Network location available again\n"); 
      break; 
     case LocationProvider.OUT_OF_SERVICE: 
      textView.setText(tvTxt + "Network location out of service\n"); 
      break; 
     case LocationProvider.TEMPORARILY_UNAVAILABLE: 
      textView.setText(tvTxt 
        + "Network location temporarily unavailable\n"); 
      break; 
     } 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "Network Provider Enabled\n"); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "Network Provider Disabled\n"); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     textView.setText(textView.getText().toString() 
       + "New network location: " 
       + String.format("%9.6f", location.getLatitude()) + ", " 
       + String.format("%9.6f", location.getLongitude()) + "\n"); 
    } 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    textView = (TextView) findViewById(R.id.textview); 
    locationManager = (LocationManager) 
      getSystemService(Context.LOCATION_SERVICE); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 5000, 0, 
      networkLocationListener); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
      3000, 0, gpsLocationListener); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(networkLocationListener); 
    locationManager.removeUpdates(gpsLocationListener); 
} 
} 
+0

此代碼是否可用?你可以用這段代碼獲得經度和緯度嗎? –

+0

是的,它在工作 – blganesh101

+0

你在設備上測試過嗎?我想問的原因是我一直在嘗試找一個LocationManager.GPS_PROVIDER的工作示例。看到我的問題在這裏http://stackoverflow.com/questions/15627956/getting-current-location-from-gps –

0

使用新的位置API's,他們已經使人們更準確,消耗電池的效率更低。