
import android.media.MediaPlayer를 사용할 것이다.binder : 서비스와 클라이언트 사이의 통신을 가능하게 해주는 클래스. 여기서는 binder를 상속받아 MusicBinder라는 클래스를 만들었고, 이 클래스는 클라이언트에서 서비스 내부의 메소드를 사용할 수 있게 해준다. chat GPT의 한마디..참고
즉, MusicBinder는 MusicService 내부에서 서비스와 클라이언트 간의 통신을 중개하는 역할을 합니다. 클라이언트는 MusicService에 연결될 때 반환된 Binder 객체를 통해 서비스의 메서드를 호출할 수 있습니다. 이를 통해 서비스와 클라이언트 간의 효과적인 상호작용이 가능해집니다.
class MusicService : Service() {
// MediaPlayer 객체 생성
private var mediaPlayer: MediaPlayer? = null
// binder 생성
private val binder = MusicBinder()
// binder를 상속받은 MusicBinder 클래스.
inner class MusicBinder: Binder() {
fun getService():MusicService = this@MusicService
}
override fun onBind(intent: Intent): IBinder {
return binder
}
fun startMusic(){
if (mediaPlayer == null){
mediaPlayer = MediaPlayer.create(this, R.raw.music)
mediaPlayer?.isLooping = true
mediaPlayer?.start()
}else{
mediaPlayer?.start()
}
}
fun stopMusic(){
mediaPlayer?.stop()
mediaPlayer?.release()
mediaPlayer = null
}
}
inner class MusicBinder
: MusicBinder 내부 클래스가 MusicService 클래스의 프라이빗 멤버에 접근할 수 있기 때문에 보안 측면에서도 안전
클라이언트에서 MusicService의 MusicBinding 클래스를 가져와서 getService() 함수를 호출해 사용할 서비스를 가져올 수 있다.
bindService() 함수 실행. override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val intent = Intent(this, MusicService::class.java)
bindService(intent, connection, Context.BIND_AUTO_CREATE)
binding.playButton.setOnClickListener {
musicService?.startMusic()
}
binding.stopButton.setOnClickListener {
musicService?.stopMusic()
}
}
bindService()가 호출될 때 자동으로 실행되는 onServiceConnected에서는 바인더를 통해 MusicService 객체를 가져온다. unBindService()가 호출될 때 자동으로 실행되는 onServiceDisconnected에서는 연결 상태를 false로 바꿔준다. private var musicService: MusicService? = null
private var isBound = false
private val connection = object: ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val binder = service as MusicService.MusicBinder
musicService = binder.getService()
isBound = true
}
override fun onServiceDisconnected(name: ComponentName?) {
isBound = false
}
}
unbindService를 통해 음악 재생을 멈춘다. override fun onDestroy() {
super.onDestroy()
if (isBound){
unbindService(connection)
isBound = false
}
}
(1) 백그라운드 단에서 실행하고 싶은게 있으면 인텐트에 담아서 bindService() 실행
(2) bindService() 호출되면 ServiceConnected() 호출되면서, getService()로 서비스 객체를 가져옴 (생명주기 상 MusicService의 onBind()가 실행되면서 binder가 반환됨.)
(3) 가져온 서비스 객체에 연결된 메소드를 실행하며 백그라운드단 제어
