How To Get Information About The Satellites From 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 onC
Solution 1:
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...
publicclassMyActivityextendsActivityimplementsGpsStatus.Listener {
LocationManagerlocationManager=null;
TextViewtv=null;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
tv = (TextView)(findViewById(R.id.Gpsinfo));
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(this);
}
@OverridepublicvoidonGpsStatusChanged(int) {
GpsStatusgpsStatus= locationManager.getGpsStatus(null);
if(gpsStatus != null) {
Iterable<GpsSatellite>satellites = gpsStatus.getSatellites();
Iterator<GpsSatellite>sat = satellites.iterator();
int i=0;
while (sat.hasNext()) {
GpsSatellitesatellite= sat.next();
strGpsStats+= (i++) + ": " + satellite.getPrn() + "," + satellite.usedInFix() + "," + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()+ "\n\n";
}
tv.setText(strGpsStats);
}
}
}
Solution 2:
Try this:
LocationManagerlocmgr= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10L, 5.0f, this);Locationlocation= 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.
Solution 3:
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
...
classYourFragment : LocationListener {privatevar mGnssStatusCallback : GnssStatus.Callback? = nullprivatelateinitvar mLocationManager: LocationManager
overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mLocationManager = requireActivity().getSystemService(LOCATION_SERVICE) as LocationManager
mGnssStatusCallback = object : GnssStatus.Callback() {
overridefunonStarted() {
Log.e(TAG, "GnssStatus.Callback onStarted")
super.onStarted()
}
overridefunonSatelliteStatusChanged(status: GnssStatus?) {
Log.e(TAG, "GnssStatus.Callback Status: ${status?.toString()}")
super.onSatelliteStatusChanged(status)
}
overridefunonStopped() {
Log.e(TAG, "GnssStatus.Callback onStopped")
super.onStopped()
}
overridefunonFirstFix(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)
}
}
overridefunonDestroyView() {
mLocationManager.removeUpdates(this)
mGnssStatusCallback?.let {
mLocationManager.unregisterGnssStatusCallback(it)
}
super.onDestroyView()
}
overridefunonLocationChanged(location: Location?) {
Log.e(TAG, "onLocationChanged new Location: ${location.toString()}")
}
overridefunonStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
Log.e(TAG, "onStatusChanged p0 ${p0 ?: "null"} - p1 ${p1}")
}
overridefunonProviderEnabled(p0: String?) {
Log.e(TAG, "onProviderEnabled p0 ${p0 ?: "null"}")
}
overridefunonProviderDisabled(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.
Post a Comment for "How To Get Information About The Satellites From Android?"