我知道有人问过这个问题,但我做不到。
此代码继续到请求位置部分。我知道现在的设置是糟糕的选择,但我只是想获得结果我现在不关心手机电池。我可以在 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 always returning null. – Overload119 50 mins ago
What happens when you step through the rest of your code within the IF statement? – Jack 44 mins ago
I eventually get to this line: lm.requestLocationUpdates(provider, 0, 0, locationListener); Then in the debugger console message I can see Latitude, Longitude, Accuracy showing -- and its showing repeatedly, so I'm assuming that the GPS has been activated and its getting the location. The problem is the locationListener is not firing any of its methods. – Overload119 41 mins ago
And I looked at the link, my code is the same. – Overload119 41 mins ago What is the value of provider at that point? – Jack 30 mins ago
The value of the provider is "gps" – Overload119 15 mins ago
Can you set some breakpoints in onLocationChanged to see when/if it is firing? If you have already done this, after running your application it may take a few seconds after registering the listener for it to fire. Move your phone around some and wait. At some point it should fire repeatedly. – Jack 12 mins agoimm.io/8vXb Here is a screenshot -- the debugger shows that the GPS is working (note that this event pops up over and over again). I already had breaks point on onLocationChanged() -- it never reaches it even after waiting for an extended period of time. The latitude/longitude it returns is always the same as well. – Overload119 39 secs ago edit
I've also tried calling lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); via a button, and click it after the console messages show that a latitude/longitude were 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/