位置侦听器
我正在使用位置侦听器来获取长信息&我的 Android 应用程序的 lat 。
我正在使用以下代码:
public Location getLastKnownLocation()
{
Location location=null;
try
{
// Get the location manager
mLocMgr = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
// List all providers:
// List<String> providers = mLocMgr.getAllProviders();
Criteria criteria = new Criteria();
bestProvider = mLocMgr.getBestProvider(criteria, false);
location = mLocMgr.getLastKnownLocation(bestProvider);
if(location==null)
{
Thread.sleep(5000);
location=mLocMgr.getLastKnownLocation(bestProvider);
}
}
catch(Exception e)
{
Log.i("program", e.getMessage());
}
return location;
}
public Location updateCurrentLocation()
{
Location mLoc=null;
try
{
mLoc=getLastKnownLocation();
mlocListener = new LocListener();
mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
mLoc=getLastKnownLocation();
}
catch(Exception e)
{
Log.e("program", e.getMessage());
}
return mLoc;
}
它工作得很好。但是当我们关闭设备并再次启动时遇到问题。然后我得到的位置为空。 然而,启动谷歌地图后,它又开始工作了。 所以请你们帮我解决这个问题,因为我不想在开始我的应用程序之前启动谷歌地图。感谢您的支持!
I am using location listener to get long & lat for my android application.
I am working with the following code:
public Location getLastKnownLocation()
{
Location location=null;
try
{
// Get the location manager
mLocMgr = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
// List all providers:
// List<String> providers = mLocMgr.getAllProviders();
Criteria criteria = new Criteria();
bestProvider = mLocMgr.getBestProvider(criteria, false);
location = mLocMgr.getLastKnownLocation(bestProvider);
if(location==null)
{
Thread.sleep(5000);
location=mLocMgr.getLastKnownLocation(bestProvider);
}
}
catch(Exception e)
{
Log.i("program", e.getMessage());
}
return location;
}
public Location updateCurrentLocation()
{
Location mLoc=null;
try
{
mLoc=getLastKnownLocation();
mlocListener = new LocListener();
mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
mLoc=getLastKnownLocation();
}
catch(Exception e)
{
Log.e("program", e.getMessage());
}
return mLoc;
}
It is working absolutely fine. But facing the problem when we switched off the device and start it again. Then I am getting the location null.
However after starting the google map, it starts working again.
So Could you please guys help me out on this because I don't want start the google map before starting my application. Thanking you for support!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您关闭和打开设备电源时,您的
getLastKnownLocation
方法可能会失败。而且,我看到,您在订阅位置更新列表器之前调用它:
为什么需要调用 getLastKnownLocation() ?当您启动设备时,它不会立即工作。
当你使用谷歌地图时,它会优雅地尝试监听位置更新。而且,一旦至少获得一次位置更新,您的
getLastKnownLocation
将开始工作。我发现您的架构需要重新思考(如果您的方法名称合适),
当您的应用程序启动时,应该在某个适当的位置仅调用一次。
然后,在
onLocationChanged(Location location)
中执行您需要的任何更新Your
getLastKnownLocation
method is likely to fail when you power off and power on your device.And, I see that, you are calling it before you subscribe to location update lister:
Why do you need to call
getLastKnownLocation()
? It will not work immediately when you start your device.When you are using Google map, it is gracefully trying to listen to location update. And, once it gets atleast one location update, then your
getLastKnownLocation
will start working.I see that your architecture need re-thinking (if your method names are appropriate)
This should be called ONLY ONCE, in some appropriate location, when your app initiates.
Then, do whatever update you need in
onLocationChanged(Location location)
小飞飞位置库:
http://code.google.com/p/little-fluffy-location-库/
简单、快速、简单!
该库通知您的位置更新。
Little Flyffy Location library:
http://code.google.com/p/little-fluffy-location-library/
Simple, fast, easy!
this library notify your location updates.