1

我在某些 Android 设备上使用以下代码。非常频繁(90% 的时间)我在 75 秒内没有得到任何 gps 更新。

你对这个问题有什么建议吗?

public class GPSLogger extends AbstractLoggerComponent {

    private static GPSLogger singleton = new GPSLogger();

    private LocationManager locationManager;
    private LocationListener locationListener;

    private Context context;

    /**
     * GPSLogger.
     */
    private GPSLogger() { 

    }

    /**
     * @return singleton instance
     */
    public static GPSLogger getInstance() {
        return singleton;
    }

    /** {@inheritDoc} */
    @Override
    public void initialise(Context context) {
        this.context = context;
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        this.locationListener = new GPSLocationListener(); 
        super.initialise(context);
    }

    /** {@inheritDoc} */
    @Override
    protected void start() {
        new Thread((new Runnable() {
            /** {@inheritDoc} */
            @Override
            public void run() {
                Looper.prepare(); 
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locationListener);
                Looper.loop();
            }
        })).start();
    }

    /** {@inheritDoc} */
    @Override
    protected void stop()
    {
        this.locationManager.removeUpdates(this.locationListener);
    }

    /**
     * GPSLocationListener.
     *
     * The custom GPSLocationListener
     */
    private class GPSLocationListener implements LocationListener {

        /** {@inheritDoc} */
        @Override
        public void onLocationChanged(Location location)
        {           
            GPSSample gpsSample = new GPSSample();
            gpsSample.setAccurracy(location.getAccuracy());
            gpsSample.setBearing(location.getBearing());
            gpsSample.setLatitude(location.getLatitude());
            gpsSample.setLongitude(location.getLongitude());
            gpsSample.setSpeed(location.getSpeed());
            gpsSample.setTimestamp(System.currentTimeMillis());
            gpsSample.setAltitude(location.getAltitude());
            gpsSample.setProvider(location.getProvider());

            singleton.setChanged();
            singleton.notifyObservers(gpsSample);
        }

        /** {@inheritDoc} */
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) { }

        /** {@inheritDoc} */
        @Override
        public void onProviderEnabled(String provider) { }

        /** {@inheritDoc} */
        @Override
        public void onProviderDisabled(String provider) { }
    }

    /** {@inheritDoc} */
    @Override
    protected String getName() {
        return "GPSLogger";
    }
}
4

0 回答 0