2011-09-13 65 views
2

這裏是下面的一個代碼假設監聽GPS的更新和發送通知給主要活動Android GPS。位置監聽器並不想獲得一個修復:(

但是它似乎並不奏效:)

無錯誤,只有onLocationChanged不被調用!

有趣的是:當我運行一些其他應用程序來獲得GPS修復,然後開始我的應用程序=> onLocationChanged被調用,只有準確性降級,並最終修復丟失。

我也嘗試添加到此代碼GPS偵聽器=>它的工作原理,視圖中的衛星每秒鐘都報告,只有修復從未被指責。

這到底是怎麼回事? :)

公共類PhoneGPSpos {

private Handler myHandler = null; 
private LocationManager lm; 
private LocationListener locationListener; 
private Location currentBest; 
Context m_context; 

公共PhoneGPSpos(上下文的背景下,處理程序處理){

myHandler = handler; 
    m_context = context; 


    lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

// Bundle bundle = new Bundle(); 
// boolean xtraInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_xtra_injection",bundle); 
// boolean timeInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_time_injection",bundle); 

    currentBest = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    locationListener = new LocationListener() 
    { 

      public void onLocationChanged(Location location) 
      { 
       Log.w("Compass", "Loc"); 

      if (location != null) 
      { 
       currentBest = location; 

       Message msg = new Message(); 

       msg.arg1 = 5; 
       myHandler.sendMessage(msg); 

      } 
      } 

      public void onStatusChanged(String provider, int status, Bundle 
      extras) { 
      Log.d("Compass", "Stat"); 
      } 

      public void onProviderEnabled(String provider) { 
      Log.d("Compass", "Prov e"); 
      } 

      public void onProviderDisabled(String provider) { 
      Log.d("Compass", "Prov d"); 
     } 
    }; 
} 

public void requestGPS() 
{ 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    Log.d("Compass", "gps requested"); 
} 

public void stopGPS() 
{ 
    Log.d("Compass", "gps stopped"); 
    lm.removeUpdates(locationListener); 
} 

public synchronized Location getBest() 
{ 
    return currentBest; 
} 

}

+0

你在哪裏檢測?在室內? – Ian

+0

不是。正如我所說:「其他應用程序能夠修復」。當我切換回到我的應用程序剛纔onLocationChanged被稱爲只有準確性正在退化。並且修復最終會丟失 – user929298

回答

1

好的,弄清楚這是一個其他人也看到的問題:顯然(至少在某些手機上)如果攝像機處於活動狀態,GPS偵聽器的工作會更糟糕。

Problem with getting GPS data when using CameraPreview

Android: Losing GPS signal strength on some phones when I start up the Camera and SurfaceView

Camera Preview and GPS reading in Android

創建的Andorid問題爲http://code.google.com/p/android/issues/detail?id=20098

如果有人有一個解決方案PLS份額

+0

一定要接受你自己的答案,以便其他人可以快速找到你的解決方案。 – Ian

+0

我在同一時間請求網絡位置時看到過這種情況。試圖找到與此相關的SO問題,以防某人有解決方法... –

0
Check for permissions in your projects AndroidManifest.xml file 

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
+0

GPS_PROVIDER不需要互聯網許可。 – Ian

+0

FINE的位置在.CURSE不是。我不使用網絡。我只需要GPS – user929298

0

你去哪裏檢測?在室內? 其他應用程序使用哪些提供程序?網絡提供商?

我的猜測是,正在運行的應用程序使用網絡提供程序,並且由於您獲取的是最後一次知道的信息,它將從以前的應用程序位置ping返回該位置。網絡提供商通過給出一般位置來幫助分配gps衛星。因此,當您啓動應用程序(在運行其他應用程序之後)時,您擁有衛星,但是(因爲您可能在室內)沒有網絡,您可能會逐漸失去衛星,從而降低準確性。

道德故事:使用網絡提供商,或確保您有清晰的天空視圖來測試GPS。

+0

不是。正如我所說:「其他應用程序能夠修復」。當我切換回到我的應用程序剛纔onLocationChanged被稱爲只有準確性正在退化。並且修復最終會丟失 – user929298

+0

因此,當你調試這個應用程序,你在外面清晰的天空視野? – Ian

+0

是的。1)我開始我的應用程序。 2)等待~10分鐘 - 不修復.3)啓動GPS狀態。在幾分鐘內獲得修復4)再次運行我的應用程序。 5)看看它如何報告具有降級準確性的位置。 6)經過一段時間的修復後,丟失 – user929298