Is it possible to know my location came from gps or glonass?

后端 未结 3 1780
旧巷少年郎
旧巷少年郎 2021-01-06 07:15

Suppose the device support both gps and glonass(support at the hardware level).

Now when I get location by the android.location API, is it possible to know the hardw

3条回答
  •  不要未来只要你来
    2021-01-06 07:46

    As @DavidWasser said, it is possible to know that. I wanted to share some codes in addition to @DavidWasser 's answer.

    You should have LocationManager object:

    private LocationManager locationManager; 
    

    and you initialize that with:

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    

    Request location updates with your implements LocationListener, GpsStatus.Listener class:

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            new GPSLocationListener());
    

    GPSLocationListenerClass:

    private class GPSLocationListener implements LocationListener,
            GpsStatus.Listener {
        private boolean isGPSFix;
    
        public void onGpsStatusChanged(int event) {
            checkGlonassFeature(); // the method which checks if locations from GLONASS or GPS
            switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                if (mLastLocation != null)
                    isGPSFix = (SystemClock.elapsedRealtime() - lastGPStime) < 3000;
                if (isGPSFix) { // A fix has been acquired.
                    Toast toast = Toast.makeText(GPSLocationService.this, "GPS has a fix.", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                } else { // The fix has been lost.
                    Toast toast = Toast.makeText(GPSLocationService.this, "GPS DOES NOT have a fix.", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                Toast toast = Toast.makeText(GPSLocationService.this, "GPS got first fix.", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                isGPSFix = true;
                break;
            }
        }
    
        @Override
        public void onLocationChanged(Location location) {
            //  Do works here
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            String statusDescription = "unknown";
            switch (status) {
            case LocationProvider.OUT_OF_SERVICE:
                statusDescription = "OUT_OF_SERVICE";
                break;
            case LocationProvider.AVAILABLE:
                statusDescription = "AVAILABLE";
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                statusDescription = "TEMPORARILY_UNAVAILABLE";
                break;
            }
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            Toast toast = Toast.makeText(GPSLocationService.this, "GPS feature is active", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            Toast toast = Toast.makeText(GPSLocationService.this, "GPS feature is passive", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }
    

    and Finally your control method:

    public void checkGlonassFeature() {
        boolean isGPSFromGlonass = false;
        final GpsStatus gs = this.locationManager.getGpsStatus(null);
        final Iterable it = gs.getSatellites();
        for (GpsSatellite sat : it) {
            if(sat.usedInFix()){
                if(sat.getPrn() > 65 && sat.getPrn() < 88)
                    isGPSFromGlonass = true;
                else
                    isGPSFromGlonass = false;
            }
            else
                isGPSFromGlonass = false;
        }
        if(isGPSFromGlonass){
            Toast toast = Toast.makeText(getBaseContext(), "Location from GLONASS", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
        else{
            Toast toast = Toast.makeText(getBaseContext(), "Location from GPS", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }
    

    Note that it is only addition to above answer. Good luck.

提交回复
热议问题