Skip to content

Instantly share code, notes, and snippets.

@mizutori
Created January 5, 2019 09:43
Show Gist options
  • Select an option

  • Save mizutori/fa4eabb621bdbcd430a403bf02743dac to your computer and use it in GitHub Desktop.

Select an option

Save mizutori/fa4eabb621bdbcd430a403bf02743dac to your computer and use it in GitHub Desktop.
AndroidLocationStarterKitKotlin_MainActivity_Initial
class MainActivity : AppCompatActivity() {
var locationService: LocationService? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val locationService = Intent(this.application, LocationService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.application.startForegroundService(locationService)
} else {
this.application.startService(locationService)
}
this.application.bindService(locationService, serviceConnection, Context.BIND_AUTO_CREATE)
}
private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
val name = className.className
if (name.endsWith("LocationService")) {
locationService = (service as LocationService.LocationServiceBinder).service
[email protected]?.startUpdatingLocation()
}
}
override fun onServiceDisconnected(className: ComponentName) {
if (className.className == "LocationService") {
[email protected]?.stopUpdatingLocation()
locationService = null
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment