getLastKnownLocation 返回null

在使用LocationManager.getLastKnownLocation("gps")获取gps定位的过程中老是报空指针异常

在网上百度查了不少资料发现这个问题多出现在2.0以上版本
解决方法多是:
1.在AndroidManifest.xml中添加

Xml代码 复制代码
  1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  2. <uses-permission android:name="android.permission.INTERNET" />  
  3. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  


2.在emulator control中手动设置经纬度

但是我的程序这些都写了依旧报空指针

后来在外国论坛上终于找到一些启发
一个哥们这样写代码

Java代码 复制代码
  1. LocationManager mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
  2. Location location = mgr.getLastKnownLocation(bundle.getString("provider"));   
  3. while(location  == null)   
  4. {   
  5.   mgr.requestLocationUpdates("gps"600001, locationListener);   
  6. }  
LocationManager mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = mgr.getLastKnownLocation(bundle.getString("provider"));
while(location  == null)
{
  mgr.requestLocationUpdates("gps", 60000, 1, locationListener);
}


我试了一下还真有效,他的解释是用debug发现getLastKnownLocation()方法不是一次就能定位的,必须多次才能成功。

但是显然这样用循环一直等待很没效率,所以我重写了MapActivity的方法

Java代码 复制代码
  1.   /**  
  2.     * 这里一定要对LocationManager进行重新设置监听  
  3.     * mgr获取provider的过程不是一次就能成功的  
  4.     * mgr.getLastKnownLocation很可能返回null  
  5.     * 如果只在initProvider()中注册一次监听则基本很难成功  
  6.     */  
  7. @Override  
  8. protected void onResume() {   
  9.     super.onResume();   
  10.     mgr.requestLocationUpdates(bundle.getString("provider"), 600001, locationListener);   
  11. }   
  12.   
  13. @Override  
  14. protected void onPause() {   
  15.     super.onPause();   
  16.     mgr.removeUpdates(locationListener);   
  17. }  
   /**
     * 这里一定要对LocationManager进行重新设置监听
     * mgr获取provider的过程不是一次就能成功的
     * mgr.getLastKnownLocation很可能返回null
     * 如果只在initProvider()中注册一次监听则基本很难成功
     */
	@Override
	protected void onResume() {
		super.onResume();
		mgr.requestLocationUpdates(bundle.getString("provider"), 60000, 1, locationListener);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mgr.removeUpdates(locationListener);
	}


一样也成功了。。

在外国技术论坛上找了个解释,个人感觉很贴切:

The call to request update for a location is not blocking, hence it wont wait there. Also the provider in emulator may not have been started.

A possible check could be to see if the settings in it disable gps provider ? then send geo fix.

However, I would use Location Listener, it would be ideal in your case since you need a geo fix to proceed further.Location Listener is Used for receiving notifications from the LocationManager when the location has changed. You can unregister the listener after first geofix.

Note: It can take some time on device to get current location, and even on device this can return null.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
如果您只申请了 `ACCESS_COARSE_LOCATION` 权限,那么可能无法通过 `getLastKnownLocation` 方法获取到位置信息,因为该方法只能获取到设备最后一次记录的位置,而不是实时位置。如果设备在获取位置之后经过了一段时间,那么这个位置可能已经过期了。 您可以尝试使用 `LocationListener` 接口来实时获取设备的位置信息。示例代码如下: ```java LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 没有权限,请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE); return; } // 设置位置提供器和最小更新时间和最小更新距离 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, new LocationListener() { @Override public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); Log.d(TAG, "Latitude: " + latitude + ", Longitude: " + longitude); // 在这里进行位置信息的处理 } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }); ``` 在这个例子中,我们使用 `LocationManager.NETWORK_PROVIDER` 作为位置提供程序,设置了最小更新时间为1秒,最小更新距离为10米,表示当设备移动超过10米或者1秒钟时间过去之后,就会获取到最新的位置信息。 请注意,由于使用的是网络位置提供程序,因此获取到的位置信息可能会有一定的误差。如果您想要获得更精确的位置信息,需要申请 `ACCESS_FINE_LOCATION` 权限,并使用 `LocationManager.GPS_PROVIDER` 作为位置提供程序来获取位置信息。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值