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