Skip to content

Instantly share code, notes, and snippets.

@DjangoLC
Created October 15, 2020 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DjangoLC/c7a300b93f6b26d3d53179e23553407a to your computer and use it in GitHub Desktop.
Save DjangoLC/c7a300b93f6b26d3d53179e23553407a to your computer and use it in GitHub Desktop.
location service
package com.google.android.gms.location.sample.locationupdates
import android.annotation.SuppressLint
import android.app.Application
import android.location.Location
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.location.*
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.OnFailureListener
import java.text.DateFormat
import java.util.*
private const val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 10000
private const val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2
private const val TAG = "LocationServiceImpl:TAG"
class LocationServiceImpl(
private val app: AppCompatActivity,
private val act: AppCompatActivity
) : LocationService {
private var mLocationCallback: LocationCallback? = null
private var mLocationSettingsRequest: LocationSettingsRequest? = null
private lateinit var mLocationRequest: LocationRequest
private var mSettingsClient: SettingsClient = LocationServices.getSettingsClient(app)
private var mFusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(app)
private var mCurrentLocation: Location? = null
private var mLastUpdateTime = ""
private var mRequestingLocationUpdates = true
override fun getLocation(): Location {
createLocationCallback()
createLocationRequest()
buildLocationSettingsRequest()
startLocationUpdates()
return Location("")
}
private fun createLocationCallback() {
mLocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
super.onLocationResult(locationResult)
mCurrentLocation = locationResult.lastLocation
mLastUpdateTime = DateFormat.getTimeInstance().format(Date())
stopLocationUpdates()
Log.e(TAG, "All location: ${mCurrentLocation?.latitude}")
//updateLocationUI()
}
}
}
private fun createLocationRequest() {
mLocationRequest = LocationRequest()
// Sets the desired interval for active location updates. This interval is
// inexact. You may not receive updates at all if no location sources are available, or
// you may receive them slower than requested. You may also receive updates faster than
// requested if other applications are requesting location at a faster interval.
mLocationRequest.interval = UPDATE_INTERVAL_IN_MILLISECONDS
// Sets the fastest rate for active location updates. This interval is exact, and your
// application will never receive updates faster than this value.
mLocationRequest.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
private fun buildLocationSettingsRequest() {
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(mLocationRequest)
mLocationSettingsRequest = builder.build()
}
@SuppressLint("MissingPermission")
private fun startLocationUpdates() {
// Begin by checking if the device has the necessary location settings.
mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(act) {
Log.i(TAG, "All location settings are satisfied.")
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper())
//updateUI()
}
.addOnFailureListener(act, OnFailureListener { e ->
/*val statusCode = (e as ApiException).statusCode
when (statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
Log.i(TAG, "Location settings are not satisfied. Attempting to upgrade " +
"location settings ")
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
val rae = e as ResolvableApiException
rae.startResolutionForResult(app., MainActivity.REQUEST_CHECK_SETTINGS)
} catch (sie: SendIntentException) {
Log.i(MainActivity.TAG, "PendingIntent unable to execute request.")
}
}
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
val errorMessage = "Location settings are inadequate, and cannot be " +
"fixed here. Fix in Settings."
Log.e(MainActivity.TAG, errorMessage)
Toast.makeText(this@MainActivity, errorMessage, Toast.LENGTH_LONG).show()
mRequestingLocationUpdates = false
}
}*/
//updateUI()
mRequestingLocationUpdates = false
})
}
private fun stopLocationUpdates() {
if (!mRequestingLocationUpdates) {
Log.d(TAG, "stopLocationUpdates: updates never requested, no-op.")
return
}
// It is a good practice to remove location requests when the activity is in a paused or
// stopped state. Doing so helps battery performance and is especially
// recommended in applications that request frequent location updates.
mFusedLocationClient.removeLocationUpdates(mLocationCallback)
.addOnCompleteListener(act, OnCompleteListener<Void?> {
mRequestingLocationUpdates = false
//setButtonsEnabledState()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment