Android Studio: 1分毎に繰り返し実行

こちらのプログラムを改造して、1分毎に、水戸の気温を取得するようにしました。
[Android Studio: 現在の天気情報取得アプリ](https://qiita.com/ekzemplaro/items/450c3aa45eaef0811368)

## 画面

```xml:res/layout/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="温度"
        android:textSize="50sp" />

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

## プログラム

```kt:MainActivity.kt
package com.example.weather01

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {
    private val handler = Handler(Looper.getMainLooper())
    private lateinit var tempTextView: TextView

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

     val cityName: String = "Mito"

        fetchApi(cityName, "metric")

               // 毎分実行
        val delayMillis: Long = 60 * 1000 // 60秒 = 1分

        handler.postDelayed(object : Runnable {
            override fun run() {
                fetchApi(cityName, "metric")
                handler.postDelayed(this, delayMillis)
            }
        }, delayMillis)
    }

    private fun fetchApi(cityName: String, units: String) {
        val moshi = Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()

        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.openweathermap.org/")
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .build()

        val tempText = findViewById<TextView>(R.id.tempText)

        println("*** fetchAPI *** aaa ***")

        thread {
            try {
                val service: WeatherService = retrofit.create(WeatherService::class.java)
                val weatherApiResponse = service.fetchWeather(
                    cityName,
                    BuildConfig.OWM_API_KEY,
                    units
                ).execute().body()
                    ?: throw IllegalStateException("bodyがnullだよ！")

                println("*** fetchAPI *** ccc ***")

                Handler(Looper.getMainLooper()).post {
                    val temperature = weatherApiResponse.main.temp.toString()
                    tempText.text = temperature
                    println(temperature)
                    Log.d("response-weather", weatherApiResponse.toString())
                }
            } catch (e: Exception) {
                Log.d("response-weather", "debug $e")
            }
        }
    }
}
```

## 実行結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/50f5c353-6984-092e-c5f5-98ead3cdf521.png)