androidgoogle-apigoogle-api-clientfusedlocationproviderapi

How can I get continuous location updates in Android?


The GoogleApiClient has been depreceted and now I am having some trouble learning the latest way to get constant location updates. Also, can I get the location updates using LocationCallback and LocationRequetMethod??


Solution

  • I am glad that I finally found it. The code is short. I have used LocationRequest and LocationCallback method. Also, I have used the fusedLocationProviderClient for requestLocationUpdates.

    public class MainActivity extends AppCompatActivity {
    
        private FusedLocationProviderClient fusedLocationProviderClient;
        TextView locationTextView;
        LocationRequest locationRequest;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            locationTextView = findViewById(R.id.location_text);
    
            fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    
    //Not the best practices to get runtime permissions, but still here I ask permissions.
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
            }
    
    //Instantiating the Location request and setting the priority and the interval I need to update the location.
            locationRequest = locationRequest.create();
            locationRequest.setInterval(100);
            locationRequest.setFastestInterval(50);
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    
    //instantiating the LocationCallBack
            LocationCallback locationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    if (locationResult != null) {
                        if (locationResult == null) {
                            return;
                        }
         //Showing the latitude, longitude and accuracy on the home screen.
                        for (Location location : locationResult.getLocations()) {
                            locationTextView.setText(MessageFormat.format("Lat: {0} Long: {1} Accuracy: {2}", location.getLatitude(),
                                    location.getLongitude(), location.getAccuracy()));
                        }
                    }
                }
            };
            fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
        }
    }
    

    In activity_main.xml

    <TextView
            android:id="@+id/location_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/andika"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    

    Also, before all these, I have added this in my dependencies

        implementation 'com.google.android.gms:play-services-location:17.0.0'
    

    And this in my Manifest

    
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    

    And this keeps on updating in intervals you choose