如何从android获取有关卫星的信息?

时间:2011-07-16 23:35:59

标签: android


我试图从android获取卫星信息并写下以下代码,但没有给出任何结果,任何人都可以指出为什么会如此?

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);
}

感谢
Nohsib

3 个答案:

答案 0 :(得分:10)

根据LocationManager.getGpsStatus(...)的文档......

  

只应从onGpsStatusChanged(int)回调调用此方法,以确保以原子方式复制数据。

尝试在您的活动上实施GpsStatus.Listener并覆盖onGpsStatusChanged(int)。实施例...

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);
        }
    }
}

答案 1 :(得分:0)

试试这个:

 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);

     }

如果有任何问题,请告诉我。

答案 2 :(得分:0)

GpsStatus.Listener已弃用。您应该改用LocationListener和GnssStatus.Callback。 以下代码:

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"}")
    }

}

注意: 我观察到GnssCallback可能会在您请求位置更新后的几分钟内到达。