全球定位系统需要连接到全球定位系统卫星,以便实时跟踪位置。问题是,有时它可能会失去连接,如果我调用
locationManager.getLastKnownLocation(provider);我得到的最后一个已知的位置可能与实际位置不同。如何在调用/使用getLastKnownLocation之前检查GPS是否已连接并更新位置
发布于 2015-03-28 14:05:41
在您的GpsStatus.Listener或Service上实现Activity接口
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;
}
}
}发布于 2015-03-28 14:05:22
为此您需要实现GpsStatus.Listener。
例如,
public class MyActivity extends Activity implements GpsStatus.Listener
{
...
...
...
// add listener into locationManager
locationManager.addGpsStatusListener(this);
@Override
public void onGpsStatusChanged(int)
{
switch (event)
{
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
break;
case GpsStatus.GPS_EVENT_FIRST_FIX: // this means you found GPS Co-ordinates
break;
case GpsStatus.GPS_EVENT_STARTED:
break;
case GpsStatus.GPS_EVENT_STOPPED:
break;
}
}
}发布于 2015-03-28 14:09:42
主席先生,我也知道这也是一个大问题。如果职位被更新或未更新,则无法检查状态。
但是,您可以使用GpsStatus.Listener。
它返回getLastKnownLocation,以前缓存的数据。最好的方法是不使用getLastKnownLocation,只需获得实时的GPS位置。
https://stackoverflow.com/questions/29318002
复制相似问题