0

在我正在制作的 gps 应用程序上更改经度和纬度似乎有一些问题。我是 Android 新手,不能 100% 确定我做对了。我还想指出,我不想仅在位置更改时才使用侦听器。我特别希望它每 5 秒更新一次,因为我在线程中有它。我似乎也没有从我在下面使用的东西中检索任何卫星数据。我只是一直在我的日志文件中写入一个空列表。我错过了许可还是代码错了?下面提供了所有需要的信息。谢谢。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize various GPS things
        location = new Location("now");
        lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        gps = lm.getGpsStatus(null);

        // Create Entry List for later use
        entries = new ArrayList<Entry>();
        rows = new ArrayList<TableRow>();

        // Turn off Stop by default
        Button button = (Button)findViewById(R.id.stopButton);
        button.setEnabled(false);

        // GPS enabled?
        final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
        }

        criteria = new Criteria();
        criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setAltitudeRequired(true);
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        backgroundThread.start();
    }

Thread thread = new Thread()
    {
        @Override
        public void run() {
            while (keepGoing) {
                // timestamp
                long ts = System.currentTimeMillis() / 1000L;

                // set location
                location = lm.getLastKnownLocation(lm.getBestProvider(criteria, true));

                // location stuff
                float ac = location.getAccuracy();
                double al = location.getAltitude();
                double lat = location.getLatitude();
                double lon = location.getLongitude();
                DataEntry d = new DataEntry(ac, al, lat, lon);

                // satellite stuff
                List<GPSSatelliteEntry> GPSentries = new ArrayList<GPSSatelliteEntry>();
                Iterable<GpsSatellite> sats = gps.getSatellites();
                Iterator<GpsSatellite> satI = sats.iterator();
                while (satI.hasNext()) {
                    GpsSatellite item = (GpsSatellite) satI.next();
                    float az = item.getAzimuth();
                    float el = item.getElevation();
                    int p = item.getPrn();
                    float s = item.getSnr();

                    GPSSatelliteEntry temp = new GPSSatelliteEntry(az, el, p, s);
                    GPSentries.add(temp);
                }

                Entry en = new Entry(ts, d, GPSentries);
                entries.add(en);
                try {
                    Thread.sleep(5000); // 5000 ms
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
            writeData();
        }
    };
4

1 回答 1

1

getLastKnownLocation 仅在设备上的某些内容正在生成位置更新时才有效。如果您的手机上没有调用 requestLocationUpdates,那么您将永远不会在 getLastKnownLocation 中更新。解决方法是调用 requestLocationUpdates 并将您的逻辑移动到这些函数中,或者甚至忽略对这些函数的调用,但无论如何都要让它们关闭。当您不再需要更新时,请记住取消注册,例如在 onStop 中。

于 2013-04-08T20:05:11.810 回答