LocationListener 适用于模拟器,不适用于手机
我无法让 LocationListener 在手机上调用 onLocationChanged() 回调。当我在模拟器中运行代码时,它工作正常,每次进行地理修复时都会调用回调。
当我在手机上运行该应用程序时,什么也没有发生。回调永远不会被调用。我的设置中同时启用了 GPS 和无线定位。该应用程序具有位置权限的所有使用权限。
另外,当我在 LocationManager 对象上调用 getLastKnownLocation() 时,我的应用程序崩溃了。 (不过,仅在我愚蠢的手机上)。即使我尝试捕获导致其崩溃的异常,它仍然会崩溃,所以我什至无法获得有关导致其崩溃的原因的任何信息。这非常令人沮丧。
LocationManager.getBestProvider() 正在返回 GPS,当我打开谷歌地图时,它会立即找到我的位置。这到底是怎么回事?有什么方法可以找出我的手机崩溃的原因吗?
private void setupLocListener(){
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(false);
lm.requestLocationUpdates(lm.getBestProvider(c,true), 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location arg0) {
map.setLocation(arg0);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
});
}
I'm having trouble getting a LocationListener to call the onLocationChanged() callback on my phone. When I run my code in the emulator, it works fine, the callback is called each time I do a geo fix.
When I run the application on my phone, nothing at all happens. The callback is never called. I have location enabled by both GPS and by Wireless in my settings. The application has all of the uses-permissions for location permissions.
Also, when I call getLastKnownLocation() on a LocationManager object, my application crashes. (Still, only on my stupid phone). Even if I try to catch an exception that's causing it to crash, it still just crashes, so I can't even get any information on what is causing it to crash. This is extremely frustrating.
LocationManager.getBestProvider() is returning GPS, and when I open google maps it finds my location in no time at all. What the heck is going on here? Is there some way I can figure out why it's crashing on my phone?
private void setupLocListener(){
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(false);
lm.requestLocationUpdates(lm.getBestProvider(c,true), 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location arg0) {
map.setLocation(arg0);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您实际开始接收 GPS 坐标之前,onLocationChanged() 不会触发。
我的意思是,根据我的经验,芯片必须预热大约一分钟左右,然后才能开始从中接收数据。
我通常会启动其他应用程序并等待它证明 GPS 芯片已预热,然后再测试任何 GPS 应用程序。
我知道您提到它在 Google 地图中工作正常,但您是否尝试过清除内存并立即重新启动应用程序?
另外, getLastKnownLocation() 始终为空,直到您开始接收坐标。
onLocationChanged() wont fire until you actually start receiving GPS coordinates.
By that I mean the chip has to warm up for about a minute or so from my experience before you start receiving data from it.
I usually start some other application and wait for it to prove that the GPS chip has warmed up before I go testing any of my GPS apps.
I know that you mentioned that it works properly in Google Maps but have you tried clearing your memory and restarting your application straight away afterwards?
Also getLastKnownLocation() is always null until you start receiving coords.
当坐标可用时,位置框架会将坐标推送到您的回调。根据天气等情况,您最初可能无法得到“修复”。当您的监听器注册成功后,您应该在状态栏上看到“GPS”指示器。
getLastKnownPosition()
工作得很好(它可能返回null
);谷歌地图在等待位置提供商的初步修复时会使用它。您可能还想查看其他可用的提供商,例如手机信号塔数据,并尝试从这些提供商(即 LKP)获取数据,而不是或直到您的“首选”提供商开始推送数据。
另外,不要假设存在任何特定服务,例如
LocationManager
(Context.getSystemService()
可以返回null
),或存在任何合适的提供程序,(getBestProvider()
可以返回null
)。您的代码将在具有正确设置的正确设备上按原样失败。如果文档显示null
,您必须检查它,否则用户将卸载它,因为它的 FC 到处都是。The Location framework pushes coordinates to your callback, when they become available. Depending on weather, etc. you may not get a "fix" initially. You should see the "GPS" indicator on the status bar when your listener is successfully registered.
getLastKnownPosition()
works just fine (it may returnnull
); and Google Maps uses that, while it is waiting for an initial fix from the location provider.You may also want to see what other providers are available, e.g. cell-tower data, and attempt to obtain data from those (i.e. LKP), either instead of, or until, your "preferred" provider starts pushing data.
Also, don't assume any particular service exists, e.g.
LocationManager
(Context.getSystemService()
can returnnull
), or any suitable provider exists, (getBestProvider()
can returnnull
). Your code will fail as-is on the right device with the right settings. If the documentation saysnull
you must check for it, or users will be uninstalling it because it FC's all over the place.