AndroidStudio: 簡単なサービス

こちらと同じことを Kotlin で行いました。
[単純なサービス](https://android.keicode.com/basics/services-simple.php)

## 環境

```xml:AndroidManifest.xml
（省略）
    <application
    （省略）
        <service android:name=".ServiceA"/>
    </application>

</manifest>
```

## 画面

```xml:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Good Morning"
        android:textSize="50sp" />

        <Button
            android:id="@+id/start_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Start"
            android:textSize="50sp" />

        <Button
            android:id="@+id/stop_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:textSize="50sp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
```

## プログラム

```MainActivity.kt
package org.ekzemplaro

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button


class MainActivity : Activity() {
    var startButton: Button? = null
    var stopButton: Button? = null
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViews()
        startButton!!.setOnClickListener {
            startService(
                Intent(
                    baseContext,
                    ServiceA::class.java
                )
            )
        }
        stopButton!!.setOnClickListener { stopService(Intent(baseContext, ServiceA::class.java)) }
    }

    protected fun findViews() {
        startButton = findViewById<View>(R.id.start_button) as Button
        stopButton = findViewById<View>(R.id.stop_button) as Button
    }
}
```

```ServiceA.kt
package org.ekzemplaro

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

class ServiceA : Service() {
    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "*** ServiceA *** onCreate ***")
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        Log.d(TAG, "*** ServiceA *** onStartCommand ***")
        return START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "*** ServiceA *** onDestroy ***")
    }

    companion object {
        const val TAG = "MyService"
    }
}
```

## 実行結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/3d3c833c-d16c-6f33-895f-5276084cc311.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/55396d06-7037-30ea-a35b-f0aecd099126.png)