-4

when i am trying to get current location using GPS Tracker ,it is tacking time only for first time but i want to get location after specific interval ? i am getting all address line by line like locality sub locality and all but after that interval for first time any solution will be appreciated..

Ankesh Roy
  • 278
  • 2
  • 8
  • 33

2 Answers2

5

Start your app service in background to get timely location updates.

public class MYService extends Service implements LocationListener {

}

and do async task on timely to get periodic location updates.

TimerTask doAsynchronousTask = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {

            }
        });
    }
};

//Starts after 20 sec and will repeat on every 20 sec of time interval.
timer.schedule(doAsynchronousTask, 20000,20000);  // 20 sec timer

it will give location updates on every 20 SEC.

skamlet
  • 693
  • 7
  • 23
Avinash Shinde
  • 385
  • 6
  • 14
  • It can be done by using GPS tracker with service as above answers or Fused location API of google and with both we can set interval as per our requirement. – Ankesh Roy Nov 24 '17 at 10:58
  • yes you can set your own time to get location updates and it needs to ON device GPS and use of fused location API for location updates. – Avinash Shinde Nov 27 '17 at 11:02
3

Try this code. make an interface GetLocation

public interface GetLocation {
    public void onLocationChanged(Location location);
    public void onStatusChanged(String s, int i, Bundle bundle);
    public void onProviderEnabled(String s);
    public void onProviderDisabled(String s);
}

then make a class CurrentLocation and implements LocationListener

public class CurrentLocation implements LocationListener {

    Context context;
    LocationManager locationManager;
    String provider;

    GetLocation getLocation;

    public CurrentLocation(Context context) {

        this.context = context;
        getLocation = (GetLocation) context;
        location();
    }

    public void location() {
        // Getting LocationManager object
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        // anruag getting last location
      //  Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        // Creating an empty criteria object
        Criteria criteria = new Criteria();

        // Getting the name of the provider that meets the criteria
        provider = locationManager.getBestProvider(criteria, false);

        if (provider != null && !provider.equals(" ")) {

            // Get the location from the given provider
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            Location location = locationManager.getLastKnownLocation(provider);

            locationManager.requestLocationUpdates(provider, 20000, 1, this);

            if (location != null)
                onLocationChanged(location);
            else {

            }
                // Toast.makeText(context, "Location can't be retrieved", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(context, "No Provider Found", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onLocationChanged(Location location) {

        // Log.e("Location", location.getProvider() + "==" + location.getAccuracy() + "==" + location.getAltitude() + "==" + location.getLatitude() + "==" + location.getLongitude());
        getLocation.onLocationChanged(location);
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude());

        ConstantValues.UPlat = String.valueOf(location.getLatitude());
        ConstantValues.UPlng = String.valueOf(location.getLongitude());

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Log.e("onStatusChanged", "==" + s);
        getLocation.onStatusChanged(s, i, bundle);
    }

    @Override
    public void onProviderEnabled(String s) {
        Log.e("onProviderEnabled", "==" + s);
        getLocation.onProviderEnabled(s);
    }

    @Override
    public void onProviderDisabled(String s) {
        Log.e("onProviderDisabled", "==" + s);
        getLocation.onProviderDisabled(s);
        // alertbox("GPS STATUS", "Your GPS is: OFF");
        // Toast.makeText(context, "Please turn on the GPS to get current location.", Toast.LENGTH_SHORT).show();

        try {

            ConstantValues.showDialogOK("Please turn on the GPS to get current location.", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    switch (i) {
                        case DialogInterface.BUTTON_POSITIVE:
                            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            context.startActivity(myIntent);
                            dialogInterface.dismiss();
                            break;
                        case DialogInterface.BUTTON_NEGATIVE:
                            dialogInterface.dismiss();
                            break;
                    }
                }
            }, context);
        } catch (Exception e) {
            Log.e("exception", e.toString()+"==");
        }

    }

}

call this class in any Activity where you want to get the current location

CurrentLocation currentLocation;

declare these two global variables for minimum distance change and time interval

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

make its object in onCreate

currentLocation = new CurrentLocation(this);

make a method

public void locationWithPermission() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (checkAndRequestPermissions()) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATES,
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new CurrentLocation(this));

        }
    }

and call this method in your Activity on any event you want to get Location