How to get satellite information from android?


I am trying to get satellite information from android and wrote this following code that does not give any results, can anyone please indicate why this is 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

+4
source share
2 answers

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

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

Try to implement GpsStatus.Listener in your activity and override 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); } } } 
+10
source

Try the following:

  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 there is any problem.

0
source

All Articles