0

我有一個簡單的位置管理器,通常工作,但是當Android設備已被關閉,然後重新打開,android位置管理器返回空,即使我有它請求更新。我知道getLastKnownLocation可以返回null,但我相信我正在處理我的代碼。所有建議讚賞。Android位置管理返回NULL

顯然lon = location.getLongitude();正在崩潰。

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location == null) 
     { 
      // request location update!! 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      lon = location.getLongitude(); 
      lat = location.getLatitude(); 
     } 


     mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     //Get last known location 
     location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     //Update if not null 
     if (location != null) 
     { 
      lat = location.getLatitude(); 
      lon = location.getLongitude(); 
     } 
     //Request update as location manager can return null otherwise 
     else 
     { 
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      lat = location.getLatitude(); 
      lon = location.getLongitude(); 
     } 
    } 
+0

這是一個愚蠢的問題,但仍然有添加的權限<使用許可權的android:NAME = 「android.permission.ACCESS_FINE_LOCATION」/> <使用許可權的android:NAME =「機器人。 permission.INTERNET「/> – Soham

+0

我有,是的! :D – ThemuRR

回答

1

唯一的問題是我在Android手機上啓用了飛行模式。我現在有點尷尬!

2

android的位置管理器返回NULL,即使我有要求的更新

正確的。請求更新請求Android 開始試圖找出設備在哪裏。這將需要一段時間。與此同時,getLastKnownLocation()可以很好地返回null

getLastKnownLocation()通常只在您想知道位置時纔有用,但如果這些數據尚未準備好,您可以繼續操作。如果您需要位置,並且getLastKnownLocation()返回null,則您將等待使用Location,直到調用onLocationChanged()。請注意,onLocationChanged()可能永遠不會被調用,因爲沒有要求用戶啓用任何位置提供程序,也沒有要求設備能夠實際獲取位置修復程序的要求。

1

我在飛機模式中看到了您的回覆,但是您的代碼中存在不良行爲。

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location == null) 
     { 
      // request location update!! 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      lon = location.getLongitude(); 
      lat = location.getLatitude(); 
     } 

你已經在這裏看到那個位置在null中,這意味着你不能訪問諸如緯度和經度等內部屬性。在任何情況下,位置請求都是異步的,它們需要很短的時間才能啓動。

 mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     //Get last known location 
     location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     //Update if not null 
     if (location != null) 
     { 
      lat = location.getLatitude(); 
      lon = location.getLongitude(); 
     } 
     //Request update as location manager can return null otherwise 
     else 
     { 
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      lat = location.getLatitude(); 
      lon = location.getLongitude(); 
     } 
    } 

即使在這裏,在「其他」克勞斯,你不能訪問該位置的時候了。這意味着你不能立即得到位置(它將只保存最後一個值)。
你應該做的是實現onLocationChanges方法,並以異步方式處理那裏的位置。像這樣:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    if (location != null) 
    { 
     lat = location.getLatitude(); 
     lon = location.getLongitude(); 
    } 
    else 
    { 
     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    } 

} 

@Override 
public void onLocationChanged(Location location) { 
    lat = location.getLatitude(); 
    lon = location.getLongitude(); 
} 
+1

非常感謝您的建議,我一定會適當地更改我的代碼! – ThemuRR