android - 在 Android 中获取 "satellites in view"和 "satellites in use"计数

标签 android gps location detect satellite

我在从 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;
}

最佳答案

明白了。

public class GpsData extends Service implements LocationListener {

boolean isGPSEnabled = false; // flag for GPS satellite status
boolean isNetworkEnabled = false; // flag for cellular network status
boolean canGetLocation = false; // flag for either cellular or satellite status

private GpsStatus mGpsStatus;
private final Context mContext;
protected LocationManager locationManager;
protected GpsListener gpsListener = new GpsListener();

Location location; // location
double dLatitude, dAltitude, dLongitude, dAccuracy, dSpeed, dSats; 
float fAccuracy, fSpeed;
long lSatTime;     // satellite time
String szSignalSource, szAltitude, szAccuracy, szSpeed;

public String szSatellitesInUse, szSatellitesInView;
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 second

public GpsData(Context context) {
    this.mContext = context;
    getLocation();  
}
class GpsListener implements GpsStatus.Listener{
    @Override
    public void onGpsStatusChanged(int event) {
    }
}   
public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);// getting GPS satellite status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);// getting cellular network status
        if (!isGPSEnabled && !isNetworkEnabled) {
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {//GPS is enabled, getting lat/long via cellular towers
                locationManager.addGpsStatusListener(gpsListener);//inserted new
                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) {
                        szAltitude = " NA (using cell towers)";
                        szSatellitesInView = " NA (using cell towers)";
                        szSatellitesInUse = " NA (using cell towers)";          
                    }
                }
            }
            if (isGPSEnabled) {//GPS is enabled, gettoing lat/long via satellite
                if (location == null) {
                    locationManager.addGpsStatusListener(gpsListener);//inserted new
                    locationManager.getGpsStatus(null);
                    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) { 
                            dAltitude = location.getAltitude();
                            szAltitude = String.valueOf(dAltitude);
                            /**************************************************************
                             * Provides a count of satellites in view, and satellites in use
                             **************************************************************/
                            mGpsStatus = locationManager.getGpsStatus(mGpsStatus);          
                            Iterable<GpsSatellite> satellites = mGpsStatus.getSatellites();
                            int iTempCountInView = 0;
                            int iTempCountInUse = 0;
                            if (satellites != null) {
                                for (GpsSatellite gpsSatellite : satellites) {
                                    iTempCountInView++;
                                    if (gpsSatellite.usedInFix()) {
                                        iTempCountInUse++;
                                    }
                                }       
                            }   
                            szSatellitesInView = String.valueOf(iTempCountInView);
                            szSatellitesInUse = String.valueOf(iTempCountInUse);
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

关于android - 在 Android 中获取 "satellites in view"和 "satellites in use"计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26148235/

相关文章:

java通过telnet发送固件

java - Android 应用无法再访问服务器文件

java - 使用 Firestore RecyclerView 加载数据导致应用崩溃

css - 如何删除放大和缩小传单 map 留下 gps 图标

android - GPS 位置改变但设备不动

android - 获取电池电量

android - 如何查看手机是否有GPS装置?

java - Android Ibeacon 室内定位体验

javascript - 是否可以使用javascript捕获浏览器地址栏中的URL更改

python - 使用 urllib2.urlopen 时如何访问包含重定向的原始响应 header