位置侦听器

发布于 2024-10-14 07:08:10 字数 1130 浏览 2 评论 0原文

我正在使用位置侦听器来获取长信息&我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赏烟花じ飞满天 2024-10-21 07:08:10

当您关闭和打开设备电源时,您的 getLastKnownLocation 方法可能会失败。

而且,我看到,您在订阅位置更新列表器之前调用它:

 mLoc=getLastKnownLocation();  //<<<<<<<<<<<<<
 mlocListener = new LocListener(); 
 mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);

为什么需要调用 getLastKnownLocation() ?当您启动设备时,它不会立即工作。

当你使用谷歌地图时,它会优雅地尝试监听位置更新。而且,一旦至少获得一次位置更新,您的 getLastKnownLocation 将开始工作。

我发现您的架构需要重新思考(如果您的方法名称合适),

当您的应用程序启动时,应该在某个适当的位置仅调用一次。

mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);

然后,在 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:

 mLoc=getLastKnownLocation();  //<<<<<<<<<<<<<
 mlocListener = new LocListener(); 
 mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);

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.

mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);

Then, do whatever update you need in onLocationChanged(Location location)

笑红尘 2024-10-21 07:08:10

小飞飞位置库:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文