GPS位置始终返回null

时间:2014-07-03 13:29:40

标签: android gps geolocation location

我有以下代码,但它总是返回location = null,那么任何人都可以帮忙吗?

public class GPSLocationService extends Service implements LocationListener {

    private LocationManager     mLocationManager;
    private boolean             mEnabled;
    private Criteria            mCriteria;
    private String              mProvider;
    private Location            mLocation;
    private Timer               mTimer;
    private LocationInfo        mLocationInfo;

    @Override
    public void onCreate() {

        super.onCreate();
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        mTimer = new Timer();

        if (mEnabled == false) {
            PhoneUtils.openGPSSettings(getApplicationContext());
        } else {

            mTimer.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {

                    mCriteria = new Criteria();
                    mProvider = mLocationManager.getBestProvider(mCriteria, false);
                    mLocation = mLocationManager.getLastKnownLocation(mProvider);
                    if (mLocation != null) {

                        onLocationChanged(mLocation);
                    } else {

                        onLocationChanged(null);
                    }

                }
            }, 0, 5000);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {

            double lat = location.getLatitude();
            double lng = location.getLongitude();

            mLocationInfo = new LocationInfo();
            mLocationInfo.setLatitude(lat);
            mLocationInfo.setLongitude(lng);
            ItamenDataController.getInstance().setLocationInfo(mLocationInfo);
        } else {

            mLocationInfo = new LocationInfo();
            mLocationInfo.setLatitude(-1);
            mLocationInfo.setLongitude(-1);
            ItamenDataController.getInstance().setLocationInfo(mLocationInfo);
        }
    }

    @Override
    public void onProviderDisabled(String provider) {

        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

1 个答案:

答案 0 :(得分:0)

您使用LocationManager错误。 getLastKnownLocation返回可能已过期的最后一个已知位置。您应该使用requestLocationUpdates来接收位置更新。