------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var soundIntent: Intent
lateinit var btnStart: Button
lateinit var btnStop: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar!!.setDisplayShowHomeEnabled(true)
supportActionBar!!.setIcon(R.drawable.music2)
title = "서비스 테스트 예제"
soundIntent = Intent(this, MusicService::class.java)
btnStart = findViewById<View>(R.id.btnStart) as Button
btnStop = findViewById<View>(R.id.btnStop) as Button
------------------------------------------------------------------------------------
***** MusicService.kt **
-- New --> Servis --> Service
-- Class Name : MusicService
------------------------------------------------------------------------------------
class MusicService : Service() {
lateinit internal var mp: MediaPlayer
override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
}
override fun onCreate() {
Toast.makeText(applicationContext, "onCreate()", Toast.LENGTH_SHORT).show()
super.onCreate()
}
override fun onDestroy() {
Toast.makeText(applicationContext, "onDestroy()", Toast.LENGTH_SHORT).show()
mp.stop()
super.onDestroy()
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Toast.makeText(applicationContext, "onStartCommand()", Toast.LENGTH_SHORT).show()