更新状态栏中有关 Android 13 上下载或导出等过程的 Android 图标通知

问题描述 投票:0回答:1

我的应用程序中有导出进度,我想更新状态栏中有关导出进度的图标。我已经像下面一样实现了它并且它有效:

private fun buildExportNotification(): Notification {
    
    val icon = getExportIcon()

    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

    val notificationBuilder = NotificationCompat
        .Builder(applicationContext, channelId)
        .setOngoing(true)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
        .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setSmallIcon(icon)
        .build()

    return notificationBuilder
}

当我的导出进度更新时,我会更新我的通知,如下所示:

    lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            exportProgressStateFlow.collect { progress ->
                exportProgress = progress.toInt()
                notification = buildExportNotification()
                with(NotificationManagerCompat.from(applicationContext)) {
                    notify(NOTIFICATION_FOREGROUND_ID, notification)
                }
            }
        }
    }

但是第一个图标是可见的,之后当我的导出进度更新时,状态栏内的图标不会更新,并且在此进度期间第一个图标始终可见并且不会改变。我需要在导出过程中更新图标。

android notifications statusbar background-service
1个回答
0
投票

为了确保导出进度发生变化时状态栏中的图标能够正确更新,您需要确保每当进度发生变化时都会更新通知的小图标。您可以通过以下方式修改代码来实现此目的:

private var notificationBuilder: NotificationCompat.Builder? = null

private fun buildExportNotification(): Notification {
    val icon = getExportIcon()

    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

    notificationBuilder = NotificationCompat
        .Builder(applicationContext, channelId)
        .setOngoing(true)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
        .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setSmallIcon(icon)

    return notificationBuilder!!.build()
}

// Update notification when export progress changes
lifecycleScope.launch {
    repeatOnLifecycle(Lifecycle.State.STARTED) {
        exportProgressStateFlow.collect { progress ->
            exportProgress = progress.toInt()
            notificationBuilder?.setSmallIcon(getExportIcon()) // Update small icon
            val updatedNotification = notificationBuilder?.build()
            if (updatedNotification != null) {
                notification = updatedNotification
                with(NotificationManagerCompat.from(applicationContext)) {
                    notify(NOTIFICATION_FOREGROUND_ID, notification)
                }
            }
        }
    }
}

在此修改后的代码中,我添加了一个 notificationBuilder 属性来保存通知生成器的实例。然后,每当导出进度发生变化时,我都会使用 setSmallIcon() 方法更新通知生成器的小图标。最后,我用更新后的小图标重建通知,并通知通知管理器显示更新后的通知。

© www.soinside.com 2019 - 2024. All rights reserved.