0

I want to get my current location Latitude and Longitude using background service periodically after every 10 second in android.

suman
  • 19
  • 6
  • 1
    Sorry, but where is question? – Vygintas B Jan 02 '17 at 11:58
  • "i want my current latitude and longitude through background services"---- How can i get???? That is my question – suman Jan 02 '17 at 12:00
  • 1
    Please do your own research. I'm sure google is full of information on this topic – Vygintas B Jan 02 '17 at 12:16
  • Please refer http://stackoverflow.com/questions/17519198/how-to-get-the-current-location-latitude-and-longitude-in-android . Also before asking a question here, make sure that you have searched for the requirement atleast once. – Pooja Rajendran C Jan 02 '17 at 12:24
  • i asked a question before here .http://stackoverflow.com/questions/41359791/get-my-current-latitude-and-longitude-even-after-app-killed-in-android – suman Jan 02 '17 at 12:45
  • but can't get answer.so i asked again if i can get proper solution. – suman Jan 02 '17 at 12:46
  • Possible duplicate of [How do I get the current GPS location programmatically in Android?](http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) – Vygintas B Jan 02 '17 at 12:56

3 Answers3

0

First you have to write a service :

public class MyService extends Service {

    Timer timer;
    Handler yardimci;

    public static long TIME = 10000;

    private GPSControlService gpsControlService;

    @Override
    public void onCreate() {
        super.onCreate();

        gpsControlService = new GPSControlService(this) {
            @Override
            public void findLocationCompleted(Location location) {
                Log.i("Service", location.getLatitude() + " - " + location.getLongitude() + " - " + location.getAccuracy());
            }
        };
        timer = new Timer();
        yardimci = new Handler(Looper.getMainLooper());

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Log.i("Service", "GPS controlled");
                gpsControl();
            }
        }, 0, TIME);

    }

    private void gpsControl() {
        yardimci.post(new Runnable() {
            @Override
            public void run() {
                gpsControlService.run();
            }
        });
    }

    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }

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

GPSControlService :

public class GPSControlService {

    private GPSModule gpsModule;

    public GPSControlService(Service service) {

        gpsModule = new GPSModule(service);
    }

    public void run(){
        if (gpsModule.canGetLocation()) {

            if (gpsModule.getLocation() != null) {
                findLocationCompleted(gpsModule.getLocation());
            }

            gpsModule.stopUsingGPS();
        }
    }

    public void stop(){
        gpsModule.stopUsingGPS();
    }

    public void findLocationCompleted(Location location){}
}

I need to write a GPSModule When the LocationListener is implanted and it finds a location, it sends the location with getLocation() method

public Location getLocation() {
 // return location
}
Onur Ciner
  • 249
  • 2
  • 3
0

You can use Location Listener for this.

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude());

        latitude = location.getLatitude();
        longitude = location.getLongitude();

        Toast.makeText(DirectionActivity.this, message,Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(DirectionActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(DirectionActivity.this,
                "Provider disabled by the user. GPS turned off",
               Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(DirectionActivity.this,
               "Provider enabled by the user. GPS turned on",
               Toast.LENGTH_LONG).show();
    }
}

You can call this by

 private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
 private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds


LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
0

There can be written a background service to fetch the location. However it is not recommended to do so as it will drain your battery drastically.

It is highly suggested that use location services once app is running in foreground.

One must know how does location listener work?

There are two parameters- one time and
Other minimum distance to be get the location. Let's say time = 10 second and Distance = 1000 meters. Location listener will return you the location in minimum parameter. If user doesn't move the location will be updated in 10 second. If user moves 2000 meters in 10 seconds then location will be updated at each 1000 meters.

Shane
  • 748
  • 2
  • 6
  • 17
  • i need to update my current lat long even app is closed. this is my main work in project.so must implement it. – suman Jan 03 '17 at 05:52