Couldn't get user location

2019-03-05 22:33发布

问题:

I am trying to get user location either network location or gps location or both. for that i am running a background service which implements LocationListener. i added all the require permissions in the manifest file also. here is my service

public class GetLocation extends Service implements LocationListener {

    LocationManager lm;
    Location networkLocation, gpsLocation;

    @Override
    public void onCreate() {
        super.onCreate();
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Toast.makeText(getApplicationContext(), "Getting Location Via GPS",
                    Toast.LENGTH_LONG).show();
        }
        if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                    this);
            networkLocation = lm
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            Toast.makeText(getApplicationContext(),
                    "Getting location Via Network", Toast.LENGTH_LONG).show();
        }
        if (gpsLocation != null) {
            Toast.makeText(
                    getApplicationContext(),
                    "GPSLOCATION: " + gpsLocation.getLatitude() + " , "
                            + gpsLocation.getLongitude(), Toast.LENGTH_LONG)
                    .show();
        }
        if (networkLocation != null) {
            Toast.makeText(
                    getApplicationContext(),
                    "NETWORKLOCATION: " + networkLocation.getLatitude() + " , "
                            + networkLocation.getLongitude(), Toast.LENGTH_LONG)
                    .show();
        }
        if (gpsLocation == null && networkLocation == null) {
            Toast.makeText(getApplicationContext(),
                    "Couldn't get user location", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread() {
            public void run() {
                Looper.prepare();

                if (gpsLocation != null) {
                    Log.d("Usman", "GPSLOCATION: " + gpsLocation.getLatitude()
                            + " , " + gpsLocation.getLongitude());
                }
                if (networkLocation != null) {
                    Log.d("Usman",
                            "NETWORKLOCATION: " + networkLocation.getLatitude()
                                    + " , " + networkLocation.getLongitude());
                }
                if (gpsLocation == null && networkLocation == null) {
                    Log.d("Usman", "Couldn't get user lcoation");
                }
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Looper.loop();
            }
        }.start();
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
        // do something with this location

        Toast.makeText(
                getApplicationContext(),
                location.getProvider() + " Location Changed:"
                        + location.getLatitude() + " , "
                        + location.getLongitude(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String arg0) {
        Toast.makeText(getApplicationContext(), arg0 + " Disabled",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), provider + " Enabled",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }

}

in onCreate() method, both gps and network locations are null. can any body tell me where am i wrong

回答1:

As you said, you are not even getting toast of "Getting location Via Network". So your execution is not going inside the if block of

if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

}

So make sure you have checked

Google's location services - Checked

in your device Settings.

Settings > location services > Google's location services and Access Location.

Then you should get location from LocationManager. Else rest of your code looks fine.

Hope this helps.

Reference link



回答2:

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

or

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000*60*1, this);


回答3:

You need to keep in mind about permissions :

1.If you want to use both NETWORK_PROVIDER and GPS_PROVIDER usepermission mentioned below.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2.if want to use only NETWORK_PROVIDER use

<uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />

3.A very common mistake can be done by normal human being. don`t forget to give Internet Permission

<uses-permission android:name="android.permission.INTERNET" />

4.The first fix for location can be long enough, the //locationManager.getLastKnownLocation(locationProvider) gives you cached location It can be null also.

You can request location based on certain Criteria:

// The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 5; // 5 minute


Criteria criteria = new Criteria();
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(false);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            String providerFine = locationManager.getBestProvider(criteria,
                    true);

            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String providerCoarse = locationManager.getBestProvider(criteria,
                    true);

            if (providerCoarse != null) {

                locationManager.requestLocationUpdates(providerCoarse,
                        MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
                        this);
            }
            if (providerFine != null) {

                locationManager.requestLocationUpdates(providerFine,
                        MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
                        this);
            }

After getting location you can check for best location fix Below method is given out here: http://developer.android.com/guide/topics/location/strategies.html

private static final int TWO_MINUTES = 1000 * 60 * 2;

/** Determines whether one Location reading is better than the current Location fix
  * @param location  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
  */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
      return provider2 == null;
    }
    return provider1.equals(provider2);
}