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

■ はじめに

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

の続き。

今回は、フォアグラウンド サービス・バックグラウンド サービスを扱う。

目次

【1】使用上の注意
【2】サンプル

【1】使用上の注意

* AndroidManifest.xml を修正する必要がある
 => [New]-[Service]からファイルを追加すれば、自動的に追加してくれる

【2】サンプル

マニフェストファイル

app/manifests/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxx.xxx.xxxx">
    <!-- Foreground Service を使う場合 -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <application
        android:theme="@style/AppTheme">
        <!-- 使用する Service を追加 -->
        <service
            android:name=".xxxx.SampleService"
            android:enabled="true"
            android:exported="true"></service>
    </application>
</manifest>

サービス

package xxx

import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import xxx.xxx.xxxxxx.R

class SampleService : Service() {
    override fun onBind(intent: Intent?): IBinder? {
        TODO("Not yet implemented")
    }

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

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

        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notification = NotificationCompat.Builder(this).apply {
            setContentTitle("Hello World")
            setContentText("Called onStartCommand")
            setSmallIcon(R.drawable.ic_menu_info_details)
        }.build()
        notificationManager.notify(1, notification)

        return START_NOT_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()

        Log.i("SampleService", "onDestroy")
    }
}

画面側

MainActivity.kt

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import xxxx.xxx.xxxxx.xxx.SampleService
import kotlinx.android.synthetic.main.activity_main.*

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

        // サービス開始
        button.setOnClickListener {
            Intent(this, SampleService::class.java).also { intent ->
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    Log.i("Activity", "startForegroundService")
                    startForegroundService(intent)
                } else {
                    Log.i("Activity", "startService")
                    startService(intent)
                }
            }
        }

        // サービス停止
        button2.setOnClickListener {
            Intent(this, SampleService::class.java).also { intent ->
                Log.i("Activity", "stopService")
                stopService(intent)
            }
        }
    }
}

参考文献

一般サイト
https://qiita.com/gksdyd88/items/30df1f220001fad69d9e
https://techbooster.org/android/application/3270/
https://qiita.com/naoi/items/03e76d10948fe0d45597
https://qiita.com/satken2/items/49dd76d848ceb208e937

関連記事

サービス ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/08/02/000000
サービス ~ 基本編 / Bind Service ~
https://dk521123.hatenablog.com/entry/2020/08/04/000000
ダイアログ表示 ~ 基本編 / Notification(通知) ~
https://dk521123.hatenablog.com/entry/2020/08/01/000000