4


I am trying to get the satellite information from the android and wrote this following code which is not giving any results, could any one point out why is it so?

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);

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

    GpsStatus gpsStatus = locationManager.getGpsStatus(null);
    if(gpsStatus != null)
    {
         Iterable<GpsSatellite>satellites = gpsStatus.getSatellites();
         Iterator<GpsSatellite>sat = satellites.iterator();
         int i=0;
         while (sat.hasNext()) {
             GpsSatellite satellite = sat.next();


             strGpsStats+= (i++) + ": " + satellite.getPrn() + "," + satellite.usedInFix() + "," + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()+ "\n\n";
         }
     }
     TextView tv = (TextView)(findViewById(R.id.Gpsinfo));
     tv.setText(strGpsStats);
}

thanks
Nohsib

3 Answers 3

11

According to the docs for LocationManager.getGpsStatus(...)...

This should only be called from the onGpsStatusChanged(int) callback to ensure that the data is copied atomically.

Try implementing GpsStatus.Listener on your Activity and overriding onGpsStatusChanged(int). Example...

public class MyActivity extends Activity implements GpsStatus.Listener {

    LocationManager locationManager = null;
    TextView tv = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gps);

        tv = (TextView)(findViewById(R.id.Gpsinfo));
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.addGpsStatusListener(this);
    }

    @Override
    public void onGpsStatusChanged(int) {
        GpsStatus gpsStatus = locationManager.getGpsStatus(null);
        if(gpsStatus != null) {
            Iterable<GpsSatellite>satellites = gpsStatus.getSatellites();
            Iterator<GpsSatellite>sat = satellites.iterator();
            int i=0;
            while (sat.hasNext()) {
                GpsSatellite satellite = sat.next();
                strGpsStats+= (i++) + ": " + satellite.getPrn() + "," + satellite.usedInFix() + "," + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()+ "\n\n";
            }
            tv.setText(strGpsStats);
        }
    }
}
3
  • Please allow me to ask a stupid questions: What is "strGpsStats"?? A string??
    – Rich
    Dec 3, 2013 at 16:49
  • @Rich : I assume so based on its 'str' prefix and the fact it's used to set the text of a TextView. I was simply modifying the OP's code from their question which had strGpsStats in it.
    – Squonk
    Dec 3, 2013 at 17:48
  • The GpsStatus is deprecated. So, how can I access the satellite info?
    – tm1701
    Jan 28, 2022 at 16:10
0

Try this:

 LocationManager locmgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
     //locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10L, 5.0f, this);
     Location location = locmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

     if ( location == null ) {
         location = locmgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

     }
     if ( location == null ) {
         location = locmgr.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

     }

Let me know if any issue.

0

GpsStatus.Listener is deprecated. You should use LocationListener and GnssStatus.Callback instead. Here following code:

import android.content.Context.LOCATION_SERVICE
import android.content.pm.PackageManager
import android.location.GnssStatus
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
...

class YourFragment : LocationListener {
    
    private var mGnssStatusCallback : GnssStatus.Callback? = null
    private lateinit var mLocationManager: LocationManager

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mLocationManager = requireActivity().getSystemService(LOCATION_SERVICE) as LocationManager

        mGnssStatusCallback = object : GnssStatus.Callback() {
            override fun onStarted() {
                Log.e(TAG, "GnssStatus.Callback onStarted")
                super.onStarted()
            }

            override fun onSatelliteStatusChanged(status: GnssStatus?) {
                Log.e(TAG, "GnssStatus.Callback Status: ${status?.toString()}")
                super.onSatelliteStatusChanged(status)
            }

            override fun onStopped() {
                Log.e(TAG, "GnssStatus.Callback onStopped")
                super.onStopped()
            }

            override fun onFirstFix(ttffMillis: Int) {
                Log.e(TAG, "GnssStatus.Callback onFirstFix $ttffMillis")
                super.onFirstFix(ttffMillis)
            }
        }

        if (ActivityCompat.checkSelfPermission(
                requireContext(),
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            return
        }

        mGnssStatusCallback?.let {
            mLocationManager.registerGnssStatusCallback(it)
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0F, this)
        }

    }

    override fun onDestroyView() {
        mLocationManager.removeUpdates(this)
        mGnssStatusCallback?.let {
            mLocationManager.unregisterGnssStatusCallback(it)
        }

        super.onDestroyView()
    }

    override fun onLocationChanged(location: Location?) {
        Log.e(TAG, "onLocationChanged new Location: ${location.toString()}")
    }

    override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
        Log.e(TAG, "onStatusChanged p0 ${p0 ?: "null"} - p1 ${p1}")
    }

    override fun onProviderEnabled(p0: String?) {
        Log.e(TAG, "onProviderEnabled p0 ${p0 ?: "null"}")
    }

    override fun onProviderDisabled(p0: String?) {
        Log.e(TAG, "onProviderDisabled p0 ${p0 ?: "null"}")
    }

}

Note: I have observed that the GnssCallback may arrive in some minutes after you request for Location update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.