How to get information about the satellites from android?

后端 未结 3 495
粉色の甜心
粉色の甜心 2021-02-10 21:12


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?

相关标签:
3条回答
  • 2021-02-10 21:25

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-10 21:35

    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.

    0 讨论(0)
  • 2021-02-10 21:44

    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 讨论(0)
提交回复
热议问题