2013-04-08 101 views
0

似乎有一些問題讓經度和緯度改變我的GPS應用程序,我做。我是Android新手,並不是100%確定我做對了。我還想指出,我不希望僅在位置更改時使用列表器。我特別希望它每5秒鐘更新一次,因爲我已在線程中進行更新。我似乎也沒有從我在下面使用的東西中檢索任何衛星數據。我只是不斷寫一個空的列表到我的日誌文件。我是否缺少權限或代碼是否錯誤?所有需要的信息在下面提供。謝謝。無法獲取緯度/經度改變對Android應用

<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

getLastKnownLocation只有在設備上的東西正在生成位置更新工作。如果手機上沒有任何內容調用requestLocationUpdates,那麼thye將永遠不會在getLastKnownLocation中更新。解決的辦法是調用requestLocationUpdates並將您的邏輯移入這些函數,甚至忽略對這些函數的調用,但無論如何它們都會關閉。請記住在不再需要更新時取消註冊,就像在onStop中一樣。