【Android】サービス ~ 基本編 / Bind Service ~

■ はじめに

https://dk521123.hatenablog.com/entry/2020/08/02/000000
https://dk521123.hatenablog.com/entry/2020/08/03/000000

の続き。

今回は、「バインドされたサービス」を学ぶ。

■ サンプル

なんだかんだで、以下の公式サイトを参考にしたほうがよかった

https://developer.android.com/guide/components/bound-services

マニフェストファイル

* 以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2020/08/03/000000

サービス

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log

class SampleService : Service() {
    private val bind = SampleBinder()

    inner class SampleBinder : Binder() {
        fun getService(): SampleService {
            return this@SampleService
        }
    }

    override fun onBind(intent: Intent?): IBinder? {
        return this.bind
    }

    override fun onCreate() {
        Log.i("SampleService", "onCreate")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.i("SampleService", "onStartCommand")
        return START_NOT_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("SampleService", "onDestroy")
    }

    fun add(value1: Int, value2: Int) : Int {
        return value1 + value2
    }
}

画面

MainActivity.kt

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.*
import android.util.Log
import xxx.xxx.xxx.xxxx.SampleService
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), ServiceConnection {
    private var service: SampleService? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        Log.i("MainActivity", "onCreate - 1")
        bindService(
            Intent(this, SampleService::class.java),
            this, Context.BIND_AUTO_CREATE)
        Log.i("MainActivity", "onCreate - 2")

        button.setOnClickListener {
            Log.i("MainActivity", "setOnClickListener - 1")
            val value1 = this.value1EditText.text.toString().toInt()
            val value2 = this.value2EditText.text.toString().toInt()
            val result = this.service?.add(value1, value2)

            Log.i("MainActivity", "setOnClickListener - 2")
            this.textView.text = "Result = $result"
        }

        button2.setOnClickListener {
        }
    }
    
    override fun onStop() {
        super.onStop()
        Log.i("MainActivity", "onStop")
        unbindService(this)
    }

    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        Log.i("MainActivity", "onServiceConnected")
        val bind = service as SampleService.SampleBinder
        this.service = bind.getService()
    }

    override fun onServiceDisconnected(name: ComponentName?) {
        Log.i("MainActivity", "onServiceDisconnected")
        this.service = null
    }
}

ログ出力

I/MainActivity: onCreate - 1
I/MainActivity: onCreate - 2
I/SampleService: onCreate
I/MainActivity: onServiceConnected
<-- ボタン押下 -->
I/MainActivity: setOnClickListener - 1
I/MainActivity: setOnClickListener - 2
<-- 画面終了-->
I/MainActivity: onStop
I/SampleService: onDestroy

参考文献

https://qiita.com/QiitaD/items/5c313076b7b99823ce1a
https://maku77.github.io/android/service/local-bind.html
https://www.atmarkit.co.jp/ait/articles/1206/15/news124.html

関連記事

サービス ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/08/02/000000
サービス ~ 基本編 / Back/Foreground Service ~
https://dk521123.hatenablog.com/entry/2020/08/03/000000