我很难从我的 GPS 实现中获得正确的“正在使用的卫星”和“视野中的卫星”整数。我已经查看了许多相关的 Stackoverflow 线程,但没有立即得到启发。以下是我到目前为止“应该”工作的内容,但事实并非如此。我已经从不同的角度重建了几次,没有得到正在使用的卫星数量或视野中的卫星。提前致谢...
public class GpsData extends Service implements LocationListener {
private GpsStatus mGpsStatus;
private final Context mContext;
boolean isGPSEnabled = false; // flag for GPS status
boolean isNetworkEnabled = false; // flag for network status
boolean canGetLocation = false; // flag for GPS status
Location location; // location
double dLatitude, dAltitude, dLongitude, dAccuracy, dSpeed, dSats;
float fAccuracy, fSpeed;
long lSatTime; // satellite time
String szSignalSource, szAltitude, szAccuracy, szSpeed;
public String szSatellitesInView;
public String szSatellitesInUse;
public static String szSatTime;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters
private static final long MIN_TIME_BW_UPDATES = 1000; //1 sec
protected LocationManager locationManager;
protected GpsListener gpsListener = new GpsListener();
public GpsData(Context context) {
this.mContext = context;
getLocation();
locationManager.addGpsStatusListener(gpsListener);
}
class GpsListener implements GpsStatus.Listener{
@Override
public void onGpsStatusChanged(int event) {
int iCountInView = 0;
int iCountInUse = 0;
mGpsStatus = locationManager.getGpsStatus(mGpsStatus);
Iterable<GpsSatellite> satellites = mGpsStatus.getSatellites();
if (satellites != null) {
for (GpsSatellite gpsSatellite : satellites) {
iCountInView++;
if (gpsSatellite.usedInFix()) {
iCountInUse++;
}
}
}
{
{
szSatellitesInView = String.valueOf(iCountInView);
szSatellitesInUse = String.valueOf(iCountInUse);
}
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS satellite status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting cellular network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.addGpsStatusListener(gpsListener);//needed?
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Cell tower", "Cell tower");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
dLatitude = location.getLatitude();
dLongitude = location.getLongitude();
lSatTime = location.getTime();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.addGpsStatusListener(gpsListener);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
dLatitude = location.getLatitude();
dLongitude = location.getLongitude();
lSatTime = location.getTime();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}