2012-06-22 10 views
0

我已經嘗試了GPS和網絡提供商,但他們給getLastKnownLocation空位置。我也沒有得到onLocationChanged。我正在從我的公寓裏嘗試牢房信號強。我甚至可以很好地瀏覽互聯網,並從市場上下載和安裝應用程序。android getLaskKnownLocation null

我已閱讀同一主題的幾個線程,但他們的建議似乎我已經嘗試過。

這是我的代碼片段。

public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if(null == locationManager) { 
     Log.d(TAG, "location manager NULL"); 
    } 
    geocoder = new Geocoder(this); 

    // Initialize with the last known location 
    Location lastLocation = locationManager 
      .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    if (lastLocation != null) 
     onLocationChanged(lastLocation); 
    else 
     Log.i(TAG, "null LastKnownLocation"); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 
    super.onStartCommand(intent, flags, startId); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
      0,0, this); 
    Log.d(TAG, "requested location updates"); 

    return START_STICKY; 
} 

按日誌:

[email protected]:/media/sf_E_DRIVE/tmp$ adb logcat LocationNotifierService:* *:S 
--------- beginning of /dev/log/system 
--------- beginning of /dev/log/main 
I/LocationNotifierService(30866): null LastKnownLocation 
D/LocationNotifierService(30866): gps provider enabled:true 
D/LocationNotifierService(30866): network provider enabled:true 
D/LocationNotifierService(30866): onCreate 
D/LocationNotifierService(30866): requested location updates 
D/LocationNotifierService(30866): requested location updates 
D/LocationNotifierService(30866): requested location updates 

我有「開鎖接收器」所以,當我解開我的屏幕我要求這正從原木預期的位置更新。但onLocationChanged方法沒有被調用。

我已添加權限以顯示罰款和粗略。

Regards,

Miten。

+0

您是否在設備上啓用了位置服務,您是否在清單中添加了權限,如果發送日誌跟蹤,是否會收到任何錯誤?網上有很多教程可以幫助你入門。 – Orlymee

回答

0

的Android getLaskKnownLocation空

按照谷歌文檔

getLaskKnownLocation返回一個位置指示從給定的提供否則返回null獲得最後已知的位置鎖定的數據。

這意味着它顯示緩存的數據。如果你正在運行你的應用程序的設備上從未查詢位置數據之前將有空的緩存這就是爲什麼你變得空。

也沒有得到任何onLocationChanged。

確保您已啓用網絡提供商的位置。

使用以下代碼段。

if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected() || connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).isConnected()) { 

    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     Constants.CONNECTED = true; 

     locationManager.requestLocationUpdates(
     LocationManager.NETWORK_PROVIDER, 5000, 1, this); 
    } else { 
     Constants.CONNECTED = false; 

     Toast.makeText(
     context, "Please enable Wireless networks in Location Setting!!", 10000).show(); 
    } 

} 
+0

這些日誌語句顯示它們已啓用。我從代碼片段中省略了它們,使得事情變得簡單明瞭:Log.d(TAG,「gps provider enabled:」+ locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)); (TAG,「network provider enabled:」+ locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));謝謝。米滕。 – Miten

+0

以下是一些日誌語句:mitenm @ pinkydebian:/ media/sf_E_DRIVE/tmp $ adb logcat LocationNotifierService:* *:S ---------開始/ dev/log/system ----- ----開始/ dev/log/main I/LocationNotifierService(30866):null LastKnownLocation D/LocationNotifierService(30866):啓用gps提供程序:true D/LocationNotifierService(30866):啓用網絡提供程序:true D/LocationNotifierService(30866):onCreate D/LocationNotifierService(30866):請求的位置更新 D/LocationNotifierService(30866):請求的位置更新 D/LocationNotifierService(30866) – Miten