无法在Android应用上获得经纬度/经度

时间:2013-04-08 19:43:28

标签: android

在我正在制作的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();
        }
    };

1 个答案:

答案 0 :(得分:1)

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