Skip to content

Instantly share code, notes, and snippets.

@luizmello
Created April 30, 2018 11:40
Show Gist options
  • Save luizmello/b669b97f9999e41984ab1cf03b9ab92d to your computer and use it in GitHub Desktop.
Save luizmello/b669b97f9999e41984ab1cf03b9ab92d to your computer and use it in GitHub Desktop.
Kotlin background location service
import android.annotation.SuppressLint
import android.app.Service
import android.content.Intent
import android.content.IntentSender
import android.os.IBinder
import android.os.Looper
import com.google.android.gms.common.api.ResolvableApiException
import com.google.android.gms.location.*
import com.google.android.gms.tasks.Task
import javax.inject.Inject
/**
* Created by luiz on 01/03/18.
*/
class FusedPositionService : Service() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
lateinit var settingsClient: SettingsClient
lateinit var locationRequest: LocationRequest
lateinit var locationCallback: LocationCallback
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return Service.START_STICKY
}
@SuppressLint("MissingPermission")
override fun onCreate() {
super.onCreate()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
createLocationRequest()
createLocationCallBack()
}
@SuppressLint("MissingPermission")
fun createLocationRequest() {
locationRequest = LocationRequest().apply {
interval = 10000
fastestInterval = 5000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
settingsClient = LocationServices.getSettingsClient(this)
val task: Task<LocationSettingsResponse> = settingsClient.checkLocationSettings(builder.build())
task.addOnSuccessListener { locationSettingsResponse ->
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
}
task.addOnFailureListener { exception ->
if (exception is ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
//exception.startResolutionForResult(this@MainActivity,
//REQUEST_CHECK_SETTINGS)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
}
fun createLocationCallBack() {
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
super.onLocationResult(locationResult)
//Do what you want with the position here
}
}
}
override fun onDestroy() {
super.onDestroy()
fusedLocationClient.removeLocationUpdates(locationCallback)
}
}
@m-montoya
Copy link

This was a life saver. Thank you for this. Worked wonders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment