Question

I have a Service which is running every minute and is supposed to constantly track location. The Service implements LocationListener. I feel like I'm not understanding this correctly though because I'm not actually using the LocationListener methods: onLocationChanged onProviderEnabled onProviderDisabled etc

I have those methods in my Class but I don't actually do anything with them. All I'm doing is every time my service runs I call LocationManager.getLastKnownLocation for both GPS and Network Provider depending on which ones are available.

This is what my code looks like:

Service runs every minute:

Intent i = new Intent(context, GPSTracker.class);
    PendingIntent pi = PendingIntent.getService(context, 0, i, 0);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if(isOn) {
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), POLL_INTERVAL, pi);
    } else {
        alarmManager.cancel(pi);
        pi.cancel();
    }

I handle the Intent:

protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "In onHandleIntent!");
    Location currentLocation = getLocation(this);
    if(currentLocation == null) {
        saveLocation(-8.0, -8.0, "nothing", "nothing", "nothing");
    } else {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
            String city = addresses.get(0).getLocality();
            String country = addresses.get(0).getCountryCode();
            String state = addresses.get(0).getAdminArea();
            saveLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), city, state, country);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And getLocation() does the real work:

 public Location getLocation(Context context) {
    try {
        locationManager = (LocationManager) context
                .getSystemService(LOCATION_SERVICE);

        // 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 {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                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
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

Something doesn't seem right though. For one, like I said I don't use any LocationListener methods, and also it doesn't seem to be very accurate. When I test on my phone sitting at my computer desk it gets it spot on every time. However, today I had this running on my phone once at work and another time when I was walking down the street and the locations it got for these places was like a mile away from both of them. Weirdly, it got the exact same wrong location for both of these places. These were both with Network providers as I didn't have my GPS on.

What am I doing wrong, and if I'm not doing anything wrong, how can I get this to be more accurate?

Was it helpful?

Solution

getLastKnownLocation () will provide the last known location(not the current location).The location could be out-of-date, if the device was turned off and moved to another location.

You need to use LocationClient to get the current location using Google Location Service. It simple to integrate. You need to create a instance to LocationClient and connect. There are callback method when the connection is established.Once connected you can retrieve the current location from mLocationClient.getLastLocation() (Returns the best most recent location currently available).

check this link https://developer.android.com/training/location/retrieve-current.html

OTHER TIPS

As your concern is to get more accurate result then you should use GPS. You can simply add a GPS enable button to ask the user for turning on the GPS.

gpsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                isGpsON = true;
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

        }
    });

You didn't use any LocationListener but I think you should use that to take the location update on

@Override
public void onProviderEnabled(String provider) {
    getLocation(this);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top