androidgpslocationmanagerandroid-gpslocationlistener

Location updates constantly in android instead of once


I was following a tutorial on how to get users location, the problem is that I am supposed to get the location coordinates only once and not infinite amount of times when I send the location, but I am constantly getting location coordinates.

The tutorial guy is getting location only when he sends the location and not constantly like me. Why does this happen and how can I fix this?

I want to get location data only once in log when the user changes location and not infinite times. Please Help.

I'm using:- Android Studio version:- 4.0.1

AVD:- Pixel 2 API 26

Here is the code:-

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    LocationListener locationListener;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.i("Location",location.toString());
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }
} 

Here is the logcat from android studio:-

2020-10-16 12:39:46.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m56s930ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:46.240 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m56s930ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:47.238 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m57s930ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:47.243 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m57s931ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:48.245 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m58s931ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:48.248 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m58s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:49.238 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m59s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:49.241 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m59s930ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:50.231 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m0s926ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:50.235 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m0s927ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:51.236 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m1s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:51.243 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m1s929ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m2s929ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.240 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m2s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.273 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m2s967ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.277 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m2s967ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:53.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m3s929ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]

Solution

  • There is no way to do this. All what can you do is write wrapper above it and send location coordinate when the values changes.