-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimerService.kt
More file actions
145 lines (127 loc) · 4.86 KB
/
Copy pathTimerService.kt
File metadata and controls
145 lines (127 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.runnect.runnect.presentation.run
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.runnect.runnect.R
import com.runnect.runnect.data.dto.TimerData
import com.runnect.runnect.util.ForegroundServiceUtil
import com.runnect.runnect.util.IntentUtil
import timber.log.Timber
import java.util.Timer
import java.util.TimerTask
class TimerService : Service() {
private val binder = LocalBinder()
private var timer: Timer? = null
private var time = 0
private var player: MediaPlayer? = null
private lateinit var notificationBuilder: NotificationCompat.Builder
private val NOTI_ID = 1 // 알림 ID
inner class LocalBinder : Binder() {
fun getService(): TimerService = this@TimerService
}
// 서비스 시작 시 호출
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
notifyStartRun()
startTimer()
initNotification()
return START_STICKY
}
fun notifyStartRun() {
if (player == null) {
player = MediaPlayer.create(this@TimerService, R.raw.start_run)
player?.setOnCompletionListener { mediaPlayer ->
mediaPlayer.release() // 재생이 끝나면 MediaPlayer 객체를 해제합니다.
}
}
player?.start()
}
fun startTimer() {
timer = Timer()
timer?.scheduleAtFixedRate(object : TimerTask() {
override fun run() {
time++
val hour = time / 3600
val minute = (time % 3600) / 60
val second = time % 60
val timerValue = String.format("%02d:%02d:%02d", hour, minute, second)
Timber.tag("Timer").d(timerValue)
// 결과를 Activity로 전달
val intent = Intent(TIMER_UPDATE_ACTION)
intent.putExtra(
EXTRA_TIMER_VALUE, TimerData(
hour = hour,
minute = minute,
second = second
)
)
sendBroadcast(intent)
// 알림의 내용 업데이트
notificationBuilder.setContentText(timerValue)
val notificationManager =
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTI_ID, notificationBuilder.build())
}
}, 0, 1000)
}
fun stopTimer() {
timer?.cancel()
}
override fun onBind(intent: Intent?): IBinder {
return binder
}
fun initNotification() {
notificationBuilder = NotificationCompat.Builder(this, "default")
.setSmallIcon(R.drawable.ic_notification_run)
.setColor(ContextCompat.getColor(this@TimerService, R.color.M1))
.setContentTitle("러닝")
.setContentText("00:00:00")
.setOnlyAlertOnce(true)
.setOngoing(true) // true 일 경우 알림 리스트에서 클릭하거나 좌우로 드래그해도 사라지지 않음
val notificationIntent = Intent(this@TimerService, RunActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
val pendingIntent = IntentUtil.createSafePendingIntent(
this@TimerService,
0,
notificationIntent,
FLAG_IMMUTABLE,
false
)
notificationBuilder.setContentIntent(pendingIntent) // 알림 클릭 시 이동
// 알림 표시
val notificationManager =
this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(
NotificationChannel(
"default",
"기본 채널",
NotificationManager.IMPORTANCE_DEFAULT
)
)
}
notificationManager.notify(
NOTI_ID,
notificationBuilder.build()
) // id : 정의해야하는 각 알림의 고유한 int값
val notification = notificationBuilder.build()
ForegroundServiceUtil.startForegroundSafely(
this,
NOTI_ID,
notification,
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
)
}
companion object {
const val TIMER_UPDATE_ACTION = "TIMER_UPDATE_ACTION"
const val EXTRA_TIMER_VALUE = "EXTRA_TIMER_VALUE"
}
}