Android 地理定位在 2.2 中有效,但在 2.3 中无效

发布于 2024-10-22 12:34:45 字数 1307 浏览 3 评论 0原文

我希望有人能对此有所启发。我试图弄清楚为什么我的 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 技术交流群。

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

发布评论

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

评论(2

有深☉意 2024-10-29 12:34:45

如果没有先前的位置,getLastKnownLocation 可以返回 null。如果是这样,您需要使用您注册的监听器等待 onLocationChanged 中的值。

getLastKnownLocation can return null if there is no previous location. If so, you need to wait for the value in onLocationChanged with the listener you registered.

寄风 2024-10-29 12:34:45

看来 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);

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