getLastKnownLocation 返回null
2016-04-13 17:37
483 查看
在使用LocationManager.getLastKnownLocation("gps")获取gps定位的过程中老是报空指针异常
在网上百度查了不少资料发现这个问题多出现在2.0以上版本
解决方法多是:
1.在AndroidManifest.xml中添加
Xml代码
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
[xml] view
plain copy
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2.在emulator control中手动设置经纬度
但是我的程序这些都写了依旧报空指针
后来在外国论坛上终于找到一些启发
一个哥们这样写代码
Java代码
LocationManager mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = mgr.getLastKnownLocation(bundle.getString("provider"));
while(location == null)
{
mgr.requestLocationUpdates("gps", 60000, 1, locationListener);
}
[java] view
plain copy
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代码
/**
* 这里一定要对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);
}
[java] view
plain copy
/**
* 这里一定要对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.
转自:http://blog.csdn.net/L_serein/article/details/6127300
在网上百度查了不少资料发现这个问题多出现在2.0以上版本
解决方法多是:
1.在AndroidManifest.xml中添加
Xml代码
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
[xml] view
plain copy
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2.在emulator control中手动设置经纬度
但是我的程序这些都写了依旧报空指针
后来在外国论坛上终于找到一些启发
一个哥们这样写代码
Java代码
LocationManager mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = mgr.getLastKnownLocation(bundle.getString("provider"));
while(location == null)
{
mgr.requestLocationUpdates("gps", 60000, 1, locationListener);
}
[java] view
plain copy
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代码
/**
* 这里一定要对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);
}
[java] view
plain copy
/**
* 这里一定要对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.
转自:http://blog.csdn.net/L_serein/article/details/6127300
相关文章推荐
- stopPropagation, preventDefault 和 return false 的区别
- CardVIew 卡片式布局
- hive中的一些基本问题解决方法
- iOS 警告收录及科学快速的消除方法
- 一款基于HTML5的Web 3D开发工具
- Socket通讯
- HBase Rowkey的散列与预分区设计
- HDU 1281 棋盘游戏(二分图匹配)
- [bzoj2555]substring 解题报告
- Oracle 死锁的检测查询及处理
- UITextView 控制输入字数总结
- 第六周项目4-静态成员应用
- android之Notification实现
- 使用lua调用mongoDB 实例
- Docker中的网络详解以及与OpenStack网络对比
- SSH限制普通用户到家目录
- Struts2的There is not Action mapped for namespace [/] 错误
- java 内部类的使用
- 倒计时四天——2983:谁是你的潜在朋友
- SD卡的编程接口