-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmReceiver.kt
91 lines (80 loc) · 3.04 KB
/
AlarmReceiver.kt
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
package com.ywhs.climate
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
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
import androidx.media3.common.util.NotificationUtil
class AlarmService: Service() {
override fun onBind(intent: Intent): IBinder? {
// TODO: Return the communication channel to the service.
throw UnsupportedOperationException("Not yet implemented")
}
override fun onCreate() {
super.onCreate()
createNotification()
mThread!!.start()
Log.d(TAG, "onCreate")
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Log.d(TAG, "onStartCommand")
return super.onStartCommand(intent, flags, startId)
}
private var mThread: Thread? = 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
}
}
}
}
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, 0)
builder.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, builder.build()) // id : 정의해야하는 각 알림의 고유한 int값
val notification = builder.build()
startForeground(NOTI_ID, notification)
}
override fun onDestroy() {
super.onDestroy()
if (mThread != null) {
mThread!!.interrupt()
mThread = null
}
Log.d(TAG, "onDestroy")
}
companion object {
private const val TAG = "MyServiceTag"
// Notification
private const val NOTI_ID = 1
}
}