Created
January 5, 2019 09:43
-
-
Save mizutori/fa4eabb621bdbcd430a403bf02743dac to your computer and use it in GitHub Desktop.
AndroidLocationStarterKitKotlin_MainActivity_Initial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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