class MyService: Service() {
override fun onBind(intent: Intent): IBinder? {
return null
}
}
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
val intent = Intent(this, MyService::class.java)
startService(intent)
val intent = Intent(this, MyService::class.java)
stopService(intent)
μ‘ν°λΉν°
λ μΈν
νΈλ₯Ό ν΅ν΄ μλΉμ€μ λ±λ‘λ λΈλ‘λμΊμ€νΈ 리μλ²
λ₯Ό μ€νμν¨λ€. μΈν
νΈμ λΆκ° λ°μ΄ν°
λ₯Ό μ¬μ©νλ€. μλΉμ€
λ μΈν
νΈλ₯Ό ν΅ν΄ μ‘ν°λΉν°μ λ±λ‘λ λΈλ‘νΈμΊμ€νΈ 리μλ²
λ₯Ό μ€νμν¨λ€. μΈν
νΈμ λΆκ° λ°μ΄ν°
λ₯Ό μ¬μ©νλ€. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c71">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab">
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#303897">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/background"
android:layout_alignParentTop="true" />
<ImageView
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_play"
android:layout_alignParentBottom="true"
android:layout_marginBottom="36dp"
android:layout_marginLeft="24dp"
android:clickable="true"/>
<ImageView
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_stop"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="36dp"
android:layout_marginRight="24dp"
android:clickable="true"/>
</RelativeLayout>
MyService.kt
package com.tutorial.c71
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.media.MediaPlayer
import android.os.IBinder
class MyService : Service() {
lateinit var player: MediaPlayer
var receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val mode = intent?.getStringExtra("mode")
if(mode != null){
if(mode == "start"){
try{
if(player.isPlaying){
player.stop()
player.release()
}
player = MediaPlayer.create(context, R.raw.music)
player.start()
}catch (e: Exception){
e.printStackTrace()
}
}else if(mode == "stop"){
if(player.isPlaying){
player.stop()
}
}
}
}
}
override fun onCreate() {
super.onCreate()
player = MediaPlayer()
registerReceiver(receiver, IntentFilter("PLAY_TO_SERVICE"))
}
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(receiver)
}
override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
}
}
MainActivity.kt
package com.tutorial.c71
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val startButton = findViewById<ImageView>(R.id.startButton)
val stopButton = findViewById<ImageView>(R.id.stopButton)
startButton.setOnClickListener {
val intent = Intent("PLAY_TO_SERVICE")
intent.putExtra("mode", "start")
sendBroadcast(intent)
startButton.isEnabled = false
stopButton.isEnabled = true
}
stopButton.setOnClickListener {
val intent = Intent("PLAY_TO_SERVICE")
intent.putExtra("mode", "stop")
sendBroadcast(intent)
startButton.isEnabled = true
stopButton.isEnabled = false
}
val intent = Intent(this, MyService::class.java)
startService(intent)
}
override fun onDestroy() {
super.onDestroy()
val intent = Intent(this, MyService::class.java)
stopService(intent)
}
}
class MyBinder: Binder() {
fun funA(arg: Int) {
// ...
}
fun funB(arg: Int): Int {
return arg * arg
}
}
override fun onBind(intent: Intent): IBinder? {
return MyBinder() // Binderλ₯Ό μμλ°μ μμ±ν κ°μ²΄ 리ν΄
}
val connection: ServiceConnection = object: ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
serviceBinder = service as MyService.MyBinder
}
override fun onServiceDisconnected(name: ComponentName?) {
// ...
}
}
MyService.kt
package com.tutorial.c71
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.IBinder
class MyService : Service() {
inner class MyBinder: Binder() {
var player = MediaPlayer()
fun startMusic() {
try{
if(player.isPlaying){
player.stop()
player.release()
}
player = MediaPlayer.create(applicationContext, R.raw.music)
player.start()
}catch (e: Exception){
e.printStackTrace()
}
}
fun stopMusic(){
if(player.isPlaying){
player.stop()
}
}
}
override fun onBind(intent: Intent): IBinder {
return MyBinder()
}
}
MainActivity.kt
package com.tutorial.c71
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var binder: MyService.MyBinder
private val connection: ServiceConnection = object: ServiceConnection {
override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
binder = p1 as MyService.MyBinder
}
override fun onServiceDisconnected(p0: ComponentName?) {
TODO("Not yet implemented")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val startButton = findViewById<ImageView>(R.id.startButton)
val stopButton = findViewById<ImageView>(R.id.stopButton)
startButton.setOnClickListener {
binder.startMusic()
startButton.isEnabled = false
stopButton.isEnabled = true
}
stopButton.setOnClickListener {
binder.stopMusic()
startButton.isEnabled = true
stopButton.isEnabled = false
}
val intent = Intent(this, MyService::class.java)
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
override fun onDestroy() {
super.onDestroy()
unbindService(connection)
}
}