android向けのアプリでシンプルにタイマー

Androidアプリのシンプルなタイマー機能と音量調節、カウンター機能を実装するソースコードを作成しました。

<!-- activity_main.xml (レイアウトファイル) -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/timer_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="00:00"
        android:textSize="48sp" />

    <SeekBar
        android:id="@+id/volume_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100" />

    <TextView
        android:id="@+id/counter_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="カウンター: 0"
        android:textSize="24sp" />

    <Button
        android:id="@+id/start_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="スタート" />

    <Button
        android:id="@+id/stop_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ストップ" />

    <Button
        android:id="@+id/reset_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="リセット" />

</LinearLayout>
// MainActivity.kt
package com.example.simpletimer

import android.media.RingtoneManager
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private var countDownTimer: CountDownTimer? = null
    private var timerDurationSeconds: Long = 0
    private var counter = 0

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

        volume_seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                setAlarmVolume(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })

        start_button.setOnClickListener {
            startTimer()
        }

        stop_button.setOnClickListener {
            stopTimer()
        }

        reset_button.setOnClickListener {
            resetTimer()
        }
    }

    private fun startTimer() {
        timerDurationSeconds = (timer_text.text.toString().split(":").let { 
            it[0].toLong() * 60 + it[1].toLong()
        })

        countDownTimer = object : CountDownTimer(timerDurationSeconds * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val seconds = millisUntilFinished / 1000
                val minutes = seconds / 60
                val remainingSeconds = seconds % 60
                timer_text.text = "%02d:%02d".format(minutes, remainingSeconds)
            }

            override fun onFinish() {
                playAlarmSound()
                incrementCounter()
            }
        }.start()
    }

    private fun stopTimer() {
        countDownTimer?.cancel()
    }

    private fun resetTimer() {
        stopTimer()
        timer_text.text = "00:00"
        counter_text.text = "カウンター: 0"
        counter = 0
    }

    private fun setAlarmVolume(progress: Int) {
        val alarmVolume = progress.toFloat() / 100
        val audioManager = getSystemService(AUDIO_SERVICE) as android.media.AudioManager
        audioManager.setStreamVolume(
            android.media.AudioManager.STREAM_ALARM,
            (audioManager.getStreamMaxVolume(android.media.AudioManager.STREAM_ALARM) * alarmVolume).toInt(),
            0
        )
    }

    private fun playAlarmSound() {
        val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
        val ringtone = RingtoneManager.getRingtonCenter(applicationContext, alarmSound)
        ringtone.play()
    }

    private fun incrementCounter() {
        counter++
        counter_text.text = "カウンター: $counter"
    }
}

このコードでは、以下の機能が実装されています。

  • タイマーのカウントダウン表示
  • シークバーでアラーム音量の調節
  • カウンター表示とインクリメント
  • スタート、ストップ、リセットボタン

タイマーは、CountDownTimerクラスを使ってカウントダウンを行い、時間が0になるとアラーム音を鳴らしてカウンターをインクリメントします。音量調節はAudioManagerクラスを使って、シークバーの値に応じてアラームの音量を変更しています。

レイアウトはLinearLayoutを使ってシンプルに配置しており、TextViewでタイマーとカウンターを表示し、Buttonでスタートやストップなどの操作を行えるようになっています。

Androidアプリを公開するには、Google Play Consoleにアップロードする必要があります。手順は以下の通りです。

  1. Google Play開発者登録
    まだ登録していない場合は、Google Play開発者に登録します。
    https://play.google.com/apps/publish
    1人あたり$25の登録料が必要です。
  2. Android Studioプロジェクトを用意
    上記のコードをAndroid Studioで新規プロジェクトとして作成するか、既存のプロジェクトにソースを追加します。
  3. アプリを準備
    アプリのアイコン、画像リソースなどのアセットを用意します。
    また、アプリの名前、説明文、カテゴリなどのメタデータを決めます。
  4. APKファイルの生成
    Android Studioで、アプリをリリース用にビルドし、APKファイルを生成します。
    メニューから「Build > Generate Signed Bundle / APK」を選択します。
  5. Google Play Consoleにアップロード
    https://play.google.com/apps/publish にアクセスし、Google アカウントでログインします。
    「新しいアプリを作成」を選択し、アプリの情報を入力していきます。
    最後に、手順4で生成したAPKファイルをアップロードします。
  6. コンテンツ確認
    Googleによるコンテンツのレビューが行われ、問題がなければ公開されます。
    数日~数週間かかる場合があります。
  7. アプリの公開
    レビュー後、アプリを公開できるようになります。
    公開の準備ができたら、Google Play Consoleから公開できます。

アップロードの詳細な手順は、Google の公式ドキュメントをご確認ください。
https://developer.android.com/studio/publish

また、Google Play開発者登録時の$25の支払い方法など、細かい手続きについても公式ドキュメントで説明されています。

タイトルとURLをコピーしました