Android 地理定位在 2.2 中有效,但在 2.3 中无效
我希望有人能对此有所启发。我试图弄清楚为什么我的 2.2 代码突然无法在 2.3 上运行。我有点困惑。这是一直在工作的代码,但现在抛出空指针异常。
@Override
public void onStart(Intent intent, int startId) {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location l){
Log.i("MYSERVICE", "LocationChanged " + l);
}
public void onStatusChanged(String provider, int status, Bundle Extras) {}
public void onProviderEnabled(String provider){
Log.i("MYSERVICE", "ProviderEnabled " + provider);
}
public void onProviderDisabled(String provider) {}
};
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
super.onStart(intent, startId);
String location = getLocation();
}
public String getLocation() {
String provider = LocationManager.GPS_PROVIDER;
Location location = lm.getLastKnownLocation(provider);
Double lat = location.getLatitude();
Log.i("lat", lat.toString());
double lng = location.getLongitude();
String writeString = lat+"&"+lng;
return writeString;
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
我也在使用 2.3 模拟器。任何帮助将不胜感激,干杯!
I was hoping someone could shed some light on this. I'm trying to figure out why all of a sudden my code from 2.2 will not work with 2.3. I'm a little bit puzzled. This is the code that has been working, but is now throwing a null pointer exception.
@Override
public void onStart(Intent intent, int startId) {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location l){
Log.i("MYSERVICE", "LocationChanged " + l);
}
public void onStatusChanged(String provider, int status, Bundle Extras) {}
public void onProviderEnabled(String provider){
Log.i("MYSERVICE", "ProviderEnabled " + provider);
}
public void onProviderDisabled(String provider) {}
};
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
super.onStart(intent, startId);
String location = getLocation();
}
public String getLocation() {
String provider = LocationManager.GPS_PROVIDER;
Location location = lm.getLastKnownLocation(provider);
Double lat = location.getLatitude();
Log.i("lat", lat.toString());
double lng = location.getLongitude();
String writeString = lat+"&"+lng;
return writeString;
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
I am using a 2.3 emulator as well. Any help would be greatly appreciated, cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有先前的位置,
getLastKnownLocation
可以返回 null。如果是这样,您需要使用您注册的监听器等待onLocationChanged
中的值。getLastKnownLocation
can return null if there is no previous location. If so, you need to wait for the value inonLocationChanged
with the listener you registered.看来 android 2.3 不适用于 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 上的“0,0”;
您需要将这 2 个参数至少设置为 1 和 1 :
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
It seems that android 2.3 does not work with "0,0" on
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
You need to set those 2 parameters to at least 1 and 1:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);