Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get information about the satellites from android?

Tags:

android


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

like image 370
Nohsib Avatar asked Dec 21 '22 11:12

Nohsib


1 Answers

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);
        }
    }
}
like image 145
Squonk Avatar answered Jan 05 '23 09:01

Squonk