[Android Kotlin] 포그라운드 서비스 (Foreground Service)
프로그래밍/Android (Kotlin)2024. 11. 1. 18:47
반응형
[출처] https://iamjm29.tistory.com/13
자세한 설명은 출처를 확인하도록 합니다.
activity_main.xml
AndroidManifest.xml
아래 전체를 복사하지 말고, 색칠한 곳만 참고하도록 합니다.
생성된 패키지명이 다르면 테마 이름도 다르기 때문입니다.
MainActivity.kt
| package kr.sysdocu.test import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { //Button var btn_start: Button? = null var btn_stop: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn_start = findViewById(R.id.btn_start) btn_stop = findViewById(R.id.btn_stop) btn_start!!.setOnClickListener(View.OnClickListener { val serviceIntent = Intent(this@MainActivity, MyService::class.java) startService(serviceIntent) Toast.makeText(this@MainActivity, "Service start", Toast.LENGTH_SHORT).show() }) btn_stop!!.setOnClickListener(View.OnClickListener { val serviceIntent = Intent(this@MainActivity, MyService::class.java) stopService(serviceIntent) Toast.makeText(this@MainActivity, "Service stop", Toast.LENGTH_SHORT).show() }) } } |
MyService.kt
| package kr.sysdocu.test import android.app.* import android.content.Intent import android.graphics.Color import android.os.Build import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat class MyService : Service() { private var mThread: Thread? = null companion object { private const val TAG = "MyService" private const val NOTI_ID = 1 } private fun createNotification() { val builder = NotificationCompat.Builder(this, "default") builder.setSmallIcon(R.mipmap.ic_launcher) builder.setContentTitle("Foreground Service") builder.setContentText("포그라운드 서비스") builder.color = Color.RED val notificationIntent = Intent(this, MainActivity::class.java) notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP) val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE) builder.setContentIntent(pendingIntent) // 알림 클릭 시 이동 val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationManager.createNotificationChannel( NotificationChannel( "default", "기본 채널", NotificationManager.IMPORTANCE_DEFAULT ) ) } val notification = builder.build() startForeground(NOTI_ID, notification) } override fun onCreate() { super.onCreate() createNotification() mThread = object : Thread("My Thread") { override fun run() { super.run() for (i in 0..99) { Log.d(TAG, "count : $i") try { sleep(1000) } catch (e: InterruptedException) { currentThread().interrupt() break } } } } mThread!!.start() Log.d(TAG, "onCreate") } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { Log.d(TAG, "onStartCommand") return START_NOT_STICKY } override fun onDestroy() { super.onDestroy() if (mThread != null) { mThread!!.interrupt() mThread = null } Log.d(TAG, "onDestroy") } override fun onBind(intent: Intent?): IBinder? { return null } } |
반응형
'프로그래밍 > Android (Kotlin)' 카테고리의 다른 글
| Notification 알림으로 인한 Activity 중복 호출 방지 (0) | 2024.11.01 |
|---|
댓글()







