geLocations() 始终返回 null

发布于 2024-11-04 17:50:12 字数 451 浏览 1 评论 0原文

我从 GPS 或网络获取位置。问题是我已经在 HTC Desire HD 上进行了测试,它工作得很好,但现在我开始在三星 Galaxy 位置的其他设备上进行测试,结果始终为空。

这是我获取位置的代码

  locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if( currentLocation == null){
    currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

提前致谢

I'm getting the locations either from GPS or Network. The problem is that i have tested it on htc desire hd and it worked perfectly fine, but now i started testing on other devices in samsung galaxy locations are always null.

Here is my code for getting locations

  locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if( currentLocation == null){
    currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

似狗非友 2024-11-11 17:50:12

您有任何可以使用 GPS 的 API 示例吗?我认为如果应用程序已经设置了位置侦听器并等待第一个位置到达,某些手机只会向应用程序提供最后一个已知位置。你的代码不会这样做。

public class YourActivity extends Activity implements LocationListener {
    private LocationManager lm;
    @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        startLocating();
    }
    private void startLocating(){
        lm.requestLocationUpdates("gps", 0, 10, this);  
    }
    @Override
    public void onLocationChanged(Location arg0) {
        lm.removeUpdates(this);
        /*
         * put your code here
         */
    }
    @Override public void onProviderDisabled(String arg0) {}
    @Override public void onProviderEnabled(String provider) {}
    @Override public void onStatusChanged(String provider, int status, Bundle extras) {}

}

Have you got any of the API examples for GPS to work? I presume some phones will only give the app the last known location if the app has already set up a location listener, and waited for the first location to arrive. Your code wouldn't do this.

public class YourActivity extends Activity implements LocationListener {
    private LocationManager lm;
    @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        startLocating();
    }
    private void startLocating(){
        lm.requestLocationUpdates("gps", 0, 10, this);  
    }
    @Override
    public void onLocationChanged(Location arg0) {
        lm.removeUpdates(this);
        /*
         * put your code here
         */
    }
    @Override public void onProviderDisabled(String arg0) {}
    @Override public void onProviderEnabled(String provider) {}
    @Override public void onStatusChanged(String provider, int status, Bundle extras) {}

}
难忘№最初的完美 2024-11-11 17:50:12

您将需要注册一个位置更改监听器。添加如下内容:

 LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                if (location != null) {
                    doSomethingWithLocation();
                }
            }

            public void onProviderDisabled(String provider) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);

确保在请求位置更新时添加正确的等待时间。如果使用 0 而不是 1000,系统将没有足够的时间来获取新位置。

运行上述代码后调用 getLastKnownLocation() 应该可以消除 null 问题。

You will need to register a listener for location changes. Add something like the following:

 LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                if (location != null) {
                    doSomethingWithLocation();
                }
            }

            public void onProviderDisabled(String provider) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);

Make sure you add the proper wait time when requesting location updates. With a zero instead of the 1000, the system will not have enough time to get the new location.

A call to getLastKnownLocation() after running the above code should remove your null problem.

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