gpt4 book ai didi

android - onLocationChanged() 未被调用

转载 作者:太空狗 更新时间:2023-10-29 12:55:31 25 4
gpt4 key购买 nike

我知道有人问过这个问题,但我做不到。

此代码继续到请求位置部分。我知道现在的设置是糟糕的选择,但我只是想获得结果我现在不关心手机电池。我可以在 Eclipse 调试器中看到

"libloc" - "Latitude: XXXXX"    
"libloc" - "Longitude: XXXX"
"libloc" - "Accuracy: 27000"

这意味着 GPS 正在工作。但我无法获得实际位置,因为 LocationListener 中的所有方法都未触发。

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc==null){
String provider = null;

if ( lm.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
provider = LocationManager.GPS_PROVIDER;
} else {
Toast.makeText(this, "GPS not available", 3000).show();
if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
Toast.makeText(this, "Provider Not available", 3000).show();
}
}

if(provider != null) {
lm.requestLocationUpdates(provider, 0, 0, locationListener);
}

} else {
Latitude = loc.getLatitude();
Longitude = loc.getLongitude();
}

这是我的 Location Listener 对象,声明在顶部代码下方。 没有这些方法被调用(因此 Log.d 函数所以我可以添加断点,但在此监听器中没有被调用)。

double Latitude         = 0.0;
double Longitude = 0.0;

LocationListener locationListener = new LocationListener(){

@Override
public void onLocationChanged(Location location) {
Latitude = location.getLatitude();
Longitude = location.getLongitude();
}

@Override
public void onProviderDisabled(String provider) { Log.d("GPS", "HEY THERE"); }
@Override
public void onProviderEnabled(String provider) { Log.d("GPS", "HEY THERE"); }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("GPS", "HEY THERE");

}

};

附加信息,来自评论:

This is not the case. loc =lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); is alwaysreturning null. – Overload119 50 mins ago

What happens when you step through the rest of your code within the IFstatement? – Jack 44 mins ago

I eventually get to this line: lm.requestLocationUpdates(provider, 0,0, locationListener); Then in the debugger console message I can seeLatitude, Longitude, Accuracy showing -- and its showing repeatedly,so I'm assuming that the GPS has been activated and its getting thelocation. The problem is the locationListener is not firing any of itsmethods. – Overload119 41 mins ago

And I looked at the link, my code is the same. – Overload119 41 minsago What is the value of provider at that point? – Jack 30 minsago

The value of the provider is "gps" – Overload119 15 mins ago
Canyou set some breakpoints in onLocationChanged to see when/if it isfiring? If you have already done this, after running your applicationit may take a few seconds after registering the listener for it tofire. Move your phone around some and wait. At some point it shouldfire repeatedly. – Jack 12 mins ago

imm.io/8vXb Here is a screenshot-- the debugger shows that the GPS is working (note that this eventpops up over and over again). I already had breaks point ononLocationChanged() -- it never reaches it even after waiting for anextended period of time. The latitude/longitude it returns is alwaysthe same as well. – Overload119 39 secs ago edit

I've also tried callinglm.getLastKnownLocation(LocationManager.GPS_PROVIDER); via a button,and click it after the console messages show that a latitude/longitudewere received. -- the Location returned by the method is null though.– Overload119 0 secs ago edit

最佳答案

我猜测您的 Location loc 始终由 getLastKnownLocation() 设置,因此您的 if 语句永远不会计算为 true。您是否调试过以确切了解发生了什么:

Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc==null){ ... }

?

据我了解,您应该仅使用 getLastKnownLocation() 来获取初始位置,然后注册您的监听器以进行跟踪。

Here是您需要的所有信息的绝佳链接。

关于android - onLocationChanged() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7206026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com