GPS的Android – 获取定位只有一次

有没有办法一次访问GPS,而不是有一个不断检查位置更新的活套?

在我的情况下,我感兴趣的是find当前的坐标,而不是与GPS卫星的连续连接。 有没有人有任何想法如何做到这一点? 提前致谢。

Solutions Collecting From Web of "GPS的Android – 获取定位只有一次"

首先检查最后一个知道的位置是否最近。 如果没有,我相信你必须设置onLocationChanged侦听器,但是一旦你得到你的第一个有效的位置,你总是可以停止更新的stream。

加成

public class Example extends Activity implements LocationListener { LocationManager mLocationManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) { // Do something with the recent location fix // otherwise wait for the update below } else { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } } public void onLocationChanged(Location location) { if (location != null) { Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude()); mLocationManager.removeUpdates(this); } } // Required functions public void onProviderDisabled(String arg0) {} public void onProviderEnabled(String arg0) {} public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} } 

不要使用getLastKnownLocation,因为它可能返回null或旧数据。

此代码仅在按下button时才获取位置,而不是每次都按下 。 人们使用离开位置监听器在每一个实例,并扼杀电池寿命所以使用我已经张贴的代码片段做了大量的研究:

 // get the text view and buttons from the xml layout Button button = (Button) findViewById(R.id.btnGetLocation); TextView latitude = (TextView) findViewById(R.id.textview4); TextView longitude = (TextView) findViewById(R.id.textview5); final LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { mlocation = location; Log.d("Location Changes", location.toString()); latitude.setText(String.valueOf(location.getLatitude())); longitude.setText(String.valueOf(location.getLongitude())); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Status Changed", String.valueOf(status)); } @Override public void onProviderEnabled(String provider) { Log.d("Provider Enabled", provider); } @Override public void onProviderDisabled(String provider) { Log.d("Provider Disabled", provider); } }; // Now first make a criteria with your requirements // this is done to save the battery life of the device // there are various other other criteria you can search for.. criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setSpeedRequired(false); criteria.setCostAllowed(true); criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH); criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH); // Now create a location manager LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // This is the Best And IMPORTANT part Looper looper = null; // Now whenever the button is clicked fetch the location one time button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { locationManager.requestSingleUpdate(criteria, locationListener, looper); } }