2011-03-17 43 views
0

我一直在研究一種將電話的GPS位置通過電子郵件發送給用戶的應用程序 - 電話發送當前位置非常重要,而不是不久前的位置。儘管最短時間爲0,最小距離爲0,Android GPS仍未更新

我嘗試了一些修復,例如更改minTime和minDist,以及在完成時刪除(或不刪除)偵聽器。此應用程序僅在特定時間使用,但是,我希望使用最少的電池,因此保持運行不是一個好的選擇。

我有運氣的一些更新,如果我設置minTime以少量,並且永遠不會被刪除的LocationListener的,在這種情況下,我發出的第二條消息將被更新,但除此之外...

這讓我覺得我需要給它更多的時間才能獲得GPS定位 - 但是我會如何強迫它做到這一點?

考慮到這一點,我一直在嘗試這個片段:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    LocationListener locationListener = new LocationListener() { 
     @Override 
     public void onStatusChanged(String provider, int status, 
       Bundle extras) { 
      // TODO Auto-generated method stub 
     } 
     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 
     } 
     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 
     } 
     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
     } 
    };  
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long)(.000001), (float)(0), 
      locationListener);  
    String s = ""; 
    SimpleDateFormat timingFormat = new SimpleDateFormat("hh:mm"); 
    timingFormat.format(new Date()); 
    s += "The current time according to the clock is: " 
      + timingFormat.format(new Date()) + "\n"; 
    Location location = lm 
      .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    lm.removeUpdates(locationListener); 

(返回字符串s,與添加的地址)

回答

0

你應該把東西onLocationChanged做與位置更新的東西進來。

通常情況下,你首先得到一個粗略的近似值,當GPS調諧精度y變好了。爲了有效地處理這個問題,請onLocationChanged檢查位置是否足夠準確,並讓它啓動任何您想要的準確位置。

一個良好的開端是隻把

Log.v("myapp","Location changed: "+location); 
onLocationChanged()

,然後觀察輸出。