Android LocationManager网络提供程序返回null

Kam*_*łys 4 gps android locationmanager

我想用Android App获取我的GPS坐标.我开始开发,我可以获得GPS坐标,但它们并不准确.我想使用NETWORK_PROVIDER,但此提供程序的Location始终为null.更有趣的是,isProvicerEnabled返回true.

我用这个帖子的例子(最佳答案) 在这里输入链接描述

private void _getLocation() {
    // Get the location manager
    try {
        boolean isGPSEnabled = false;
        boolean isNetworkEnabled = false;
        locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        Location location = null;
        double latitude = -1;
        double longitude = -1;

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            if (isNetworkEnabled) {
                showToast("network");
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        1000,
                        0, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            else if (isGPSEnabled) {
                showToast("gps");
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            1000,
                            0, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
        showToast("" + latitude + longitude);

    } catch (Exception e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

我拥有清单中的所有权限

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)

我知道代码很脏,但现在它只用于测试.我错过了什么吗?我在很多地方找到了类似的例子,看起来很简单,所以我有点困惑.我的手机工作正常,GPS和网络工作正常.例如谷歌地图应用程序我运作良好.有什么建议?

Gab*_*han 10

请不要使用此代码.这不好.它有很多错误.此外,如果getLastKnownLocation还没有位置,它将返回null.如果手机上没有人使用requestUpdates,它总会如此.

您的代码取自发布在此处称为GPSTracker的非常旧的线程上的类.几个月来我一直试图杀死这些代码 - 它引起的问题远远超过它的帮助.如果你想要更好的示例代码,请尝试http://gabesechansoftware.com/location-tracking/这是我写的关于代码有多糟糕的博客文章.它将向您展示正确的方法,并解释该代码的一些错误.