Смена иконки foreground сервиса android при переходе в разные состояния
возник следующий вопрос. В моем приложении есть foreground сервис и привязанная к нему activity. Предположим, что у сервиса может быть несколько состояний, через которые он переходит при нажатии на кнопки в activity. Вопрос заключается в следующем: как мне менять иконку сервиса при его переходе из одного состояния в другое?
Ответы (1 шт):
Автор решения: Sam324
→ Ссылка
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when(intent?.action) {
ACTION_START_FOREGROUND_SERVICE -> startForegroundService()
ACTION_STOP_FOREGROUND_SERVICE -> stopForegroundService()
}
return super.onStartCommand(intent, flags, startId)
}
private fun startForegroundService() {
startForeground(startId, getMyActivityNotification(smallIcon = R.drawable.ic_launcher_foreground))
}
private fun getMyActivityNotification(smallIcon: Int): Notification {
val intent = Intent()
val pendingIntent: PendingIntent = PendingIntent.getActivity(this,0, intent, 0)
val b = NotificationCompat.Builder(this, channelID )
b.setContentIntent(pendingIntent)
b.setContentTitle("Title")
b.setContentText("Simple text")
b.setSmallIcon(smallIcon)
START_NOT_STICKY
return b.build()
}
private fun updateNotification() {
val smallIcon: Int = when (mConnectionState) {
ConnectionState.CONNECTED -> R.drawable.ic_thumb_up_black_24dp
ConnectionState.DISCONNECTED -> R.drawable.ic_thumb_down_black_24dp}
when (mServiceState) {
ServiceState.BUSY -> R.drawable.ic_watch_later_black_24dp
ServiceState.IDLE -> R.drawable.ic_pan_tool_black_24dp
}
val notification: Notification = getMyActivityNotification(smallIcon)
val mNotificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.notify(startId, notification)
}