0

I want to get current location for every 1 minute or every 1km change using background service. So I have written a service and my code inside the onStart() method of service is`

if(lm==null)
 lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

            //exceptions will be thrown if provider is not permitted.
            try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}
            catch(Exception ex){
                Log.d("location","Exception gps enabled....."+ex.toString());
            }
            try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}
            catch(Exception ex){
                Log.d("location","Exception network enabled....."+ex.toString());
            }



            if(gps_enabled){

                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1000,locationListenerGps);
            }
            if(network_enabled)
            {
                Log.d("location","network_enabled.....");
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 1000, locationListenerNetwork);

            }


    }
    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {

            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
            Log.d("location","Latitude from gps....."+ location.getLatitude());
            Log.d("location","Longitude from gps....."+location.getLongitude());
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {

            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
            Log.d("location","Latitude from network ....."+ location.getLatitude());
            Log.d("location","Longitude from network ....."+location.getLongitude());
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };code here

Now, When I call this service, I am Getting location only once. What is the problem ? Please help me to fix this issue

Prabhu M
  • 3,534
  • 8
  • 48
  • 87

1 Answers1

0

Use timer and run the thread for getting Current locations for every one minute.

Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19