The Google API client

Declaring, initializing, and managing the connection options of the Google API client is to be handled along the lifecyle events of Android app. We also need to get the location updates once the connection is established.

In the onStart method, we check if the mGoogleAPIClient instance is not null and request the connection be initiated:

   override fun onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}

In the onStop method, we check whether the mGoogleAPIClient instance is connected and if it is, we call the disconnect method:

    override fun onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}

In case something goes wrong and the connection gets suspended, we request a reconnect in the onConnectionSuspended method:

     override fun onConnectionSuspended(p0: Int) {

Log.i(TAG, "Connection Suspended");
mGoogleApiClient.connect();
}

In case the Google Location API cannot establish the connection, we log the reason for this by getting the error code:

     override fun onConnectionFailed(connectionResult: 
ConnectionResult) {
Log.i(TAG, "Connection failed. Error: " +
connectionResult.getErrorCode());
}

In the onConnected method, we first check for the permission to ACCESS _FINE_LOCATION and that the ACCESS_COARSE_LOCATION is indeed present in the manifest file.

We call the startLocationUpdates() method once we ensure the permissions are granted:

override fun onConnected(p0: Bundle?) {

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

return;
}
startLocationUpdates();

The fusedLocationProviderClient provides the current location details and assigns them to the mLocation variable:

var fusedLocationProviderClient :
FusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient .getLastLocation()
.addOnSuccessListener(this, OnSuccessListener<Location> {
location ->
if (location != null) {
mLocation = location;
} }) }

The startLocationUpdates creates the LocationRequest instance and provides the parameters we set for the updates. We also call the FusedLocationAPI and request the location updates:


protected fun startLocationUpdates() {
// Create the location request
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL);
// Request location updates
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}

LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}

The onLocationChanged method is the important method in which we get the details of the current location of the user. We also read the latitude and longitude the user input for the alarm from the shared preferences. Once we get both sets of details, we call the CheckAlarmLocation method which matches the latitude/longitude and alert the user as and when they reach the area of interest:

override fun onLocationChanged(location: Location) {

val sharedPref =
this?.getSharedPreferences(getString(R.string.PREFS_NAME),
Context.MODE_PRIVATE)
?: return
AlarmLat =
java.lang.Double.parseDouble(sharedPref.getString("userLat",
"13.07975"))
AlarmLong =
java.lang.Double.parseDouble(sharedPref.getString("userLang",
"80.1798347"))

UserLat = location.latitude
UserLong = location.longitude


val AlarmLat1 = AlarmLat
val AlarmLong1 = AlarmLong
val UserLat1 = UserLat
val UserLong1 = UserLong

if(AlarmLat1 != null && AlarmLong1 != null && UserLat1 != null
&& UserLong1 != null){


checkAlarmLocation(AlarmLat1,AlarmLong1,UserLat1,UserLong1)
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset