GPS需要连接到GPS卫星以实时跟踪位置。
问题是,有时它可能会失去连接,如果我调用

    locationManager.getLastKnownLocation(provider);


我得到了可能与实际位置不同的最后一个已知位置。
在调用/使用getLastKnownLocation之前如何检查GPS是否已连接以及位置已更新?

最佳答案

GpsStatus.ListenerActivity上实现Service接口:

public class LocationActivity extends Activity implements GpsStatus.Listener
{

   private LocationManager locationManager;

   @Override
   public void onCreate(Bundle savdedInstanceState)
       ....
       ....
       locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
       locationManager.addGpsStatusListener(this);
   }

   @Override
   public void onGpsStatusChanged(int event)
   {

      switch (event)
      {
        case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
        break;
        case GpsStatus.GPS_EVENT_FIRST_FIX:
        /* This is when GPS is activated; add code here for how you want the application to react. */
        break;
        case GpsStatus.GPS_EVENT_STARTED:
        break;
        case GpsStatus.GPS_EVENT_STOPPED:
        break;
      }
   }
}

10-06 14:27