user3780814
user3780814

Reputation: 147

android get location in background and update listview

I'm trying to program an application which is using the current location from the user and calculating the distance and writes it into my listview.

The location doesn't have to be very accurate and i only want to fetch a new location when the list is refreshed or on app start, not continously.

My problem is that the locationlistener with gps takes too long to find a location and i have to update my list a lot before it is showing the right distance.

I was thinking about implementing a background task which gets the location and updates the list automatically when it found the position. Would that be a solution?

Is there any option to get a location faster, even if it is not as accurate as gps?

what i have so far on my location listener:

 public class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            lat = location.getLatitude();
            lng = location.getLongitude();

            myLoc.setLatitude(lat);
            myLoc.setLongitude(lng);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }

in this method i'm calling the locationmanager and listener and creating the listview with the distance

 public void getList(){
    locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
    locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);

//... creating the list with distance and so on
}

i hope you can give me some hints how i can implement this that i will work as described above or tell me what i should use instead. thanks :)

Upvotes: 0

Views: 1632

Answers (1)

Android14
Android14

Reputation: 1125

1). You can use LocationManager.NETWORK_PROVIDER

This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.

eg:- locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener);

2). If you want to use background task then use this service

public class LocationFinder extends Service {
    public static double lat, lng;

    LocationManager locationManager;

    public void onDestroy() {

        super.onDestroy();

        if (locationManager != null && locationListener != null) {
            locationManager.removeUpdates(locationListener);
        }

    }

    @Override
    public void onCreate() {
        Log.v("location", "===>location ed  onCreate " + lat);

        super.onCreate();

    }

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

        Log.v("location", "===>location ed  onStartCommand " + lat + "==>" + lng);
        new Handler().post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                getLocation();

            }
        });
        return START_STICKY;
    }

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    private void getLocation() {
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(context);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                Location location = locationManager.getLastKnownLocation(provider);
                updateWithNewLocation(location);
                locationManager.requestLocationUpdates(provider, 500, 50, locationListener);
            } else {
                if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener);

                } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 50, locationListener);
                } else if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 500, 50, locationListener);
                }
            }

        }
    }

    private void updateWithNewLocation(Location location) {
        if (location != null) {
            Log.v("location", "===>location ed  " + lat);

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

        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

Upvotes: 1

Related Questions