簡體   English   中英

Android中的LocationManager的位置返回null

[英]LocationManager's location in Android returns null

我正在設備上測試我的Android應用,當我使用它時,它總是會失敗

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

但是,如果我使用NETWORK_PROVIDER而不是GPS_PROVIDER,它將起作用。 currentLocation返回null。

我該如何處理? 我需要在locationListener中添加什么特定內容嗎?

謝謝!

GPS_PROVIDER可能無法在有限制的地方工作,例如在房屋內或類似的地方。 您應采用以下方式進行編程:如果gps provider提供的位置為空,則可以使用network provider作為替代方案,盡管其准確性不是很高。 這里還有另一個問題。 您的代碼應如下所示:

     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
{
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}

首先,檢查設備狀態欄上的GPS圖標是否停止閃爍,以表明您實際上已鎖定GPS。

如果當前未知位置(例如,您沒有GPS鎖定),則該呼叫將返回null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM