[O] Modify instead of replace notification

This commit is contained in:
Azalea Gui
2023-01-24 13:13:50 -05:00
parent 14b014b415
commit b1097a007f
@@ -32,13 +32,18 @@ class MyService : Service()
override fun onBind(intent: Intent?) = null override fun onBind(intent: Intent?) = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int
{
init()
return super.onStartCommand(intent, flags, startId)
}
fun init()
{ {
influx = prefs.createInflux() influx = prefs.createInflux()
startCollect() startCollect()
registerReceiver(mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) registerReceiver(mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
"Just started!".notif() notif(sub = "Bluetooth Connecting...")
return super.onStartCommand(intent, flags, startId)
} }
fun startCollect() fun startCollect()
@@ -51,10 +56,10 @@ class MyService : Service()
private fun add(it: Any) = scope.launch { private fun add(it: Any) = scope.launch {
runCatching { runCatching {
Timber.d("Adding ${it.javaClass.simpleName} to influxdb") Timber.d("+${it.javaClass.simpleName}")
influx add it influx add it
"Recorded ${count.addAndGet(1)} events!".notif() notif(text = "Recorded ${count.addAndGet(1)} events!")
}.orTrace() }.orTrace()
} }
@@ -65,13 +70,16 @@ class MyService : Service()
private val intent by lazy { PendingIntent.getActivity(this, 0, intent<MainActivity>(), private val intent by lazy { PendingIntent.getActivity(this, 0, intent<MainActivity>(),
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) } PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) }
private fun String.notif() = startForeground(NOTIF_ID, private val nSub = AtomicReference<String>()
NotificationCompat.Builder(this@MyService, NOTIF_CHANNEL_ID) private val nText = AtomicReference<String>()
private fun notif(sub: String? = null, text: String? = null) = startForeground(NOTIF_ID,
NotificationCompat.Builder(this, NOTIF_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_watch_24) .setSmallIcon(R.drawable.ic_watch_24)
.setContentIntent(intent) .setContentIntent(intent)
.setContentTitle("🐱 Running!")
.setOngoing(true) .setOngoing(true)
.setSubText(this) .setContentTitle("🐱 Running!")
.setContentText(text?.also { nText.set(it) } ?: nText.get())
.setSubText(sub?.also { nSub.set(it) } ?: nSub.get())
.build()) .build())
companion object companion object