Android Application

Kotlin 서비스(Service) : 화면이 종료되어도 계속되는 음악 서비스 만들기

작성자 임베디드코리아 작성일23-10-23 23:04 조회1,403회 댓글0건

첨부파일

Kotlin 서비스(Service)
- 화면이 종료되어도 계속되는 음악 서비스 만들기
-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnStart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="음악서비스 시작" />

    <Button
        android:id="@+id/btnStop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="음악서비스 중지" />

</LinearLayout>

------------------------------------------------------------------------------------
    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

        btnStart.setOnClickListener {
            startService(soundIntent)
            Toast.makeText(applicationContext, "startService()", Toast.LENGTH_SHORT)
                .show()
            finish()
        }

        btnStop.setOnClickListener {
            stopService(soundIntent)
            Toast.makeText(applicationContext, "stopService()", Toast.LENGTH_SHORT)
                .show()
        }
    }
}

------------------------------------------------------------------------------------
*****    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()

        mp = MediaPlayer.create(this, R.raw.song2)
        mp.isLooping = true
        mp.start()
        return super.onStartCommand(intent, flags, startId)

    }
}

------------------------------------------------------------------------------------
    AndroidManifest.xml
------------------------------------------------------------------------------------
***> 추가하기 <***
        <service
            android:name=".MusicService"
            android:enabled="true"
            android:exported="true">
        </service>


------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.MusicService" parent="Theme.AppCompat.Light">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.MusicService" parent="Base.Theme.MusicService" />
</resources>