PendingIntents заполняют память - PullRequest
0 голосов
/ 14 января 2019

Я создаю приложение, которое использует службу переднего плана для отображения простого таймера обратного отсчета в уведомлении. Я использую PendingIntent с компоновщиком уведомлений, чтобы позволить пользователю вернуться в приложение, когда они нажмут на уведомление.

Все работает нормально, но каждый раз, когда таймер срабатывает, кажется, что в кеш добавляется новый PendingIntent, который занимает больше памяти.

D/Notification( 9964): allPendingIntents
D/Notification( 9964): allPendingIntents
D/Notification( 9964): allPendingIntents
D/Notification( 9964): allPendingIntents
I/zygote64( 9964): Do partial code cache collection, code=30KB, data=25KB
I/zygote64( 9964): After code cache collection, code=30KB, data=25KB
I/zygote64( 9964): Increasing code cache capacity to 128KB
D/Notification( 9964): allPendingIntents

Я не уверен, как заставить его перестать увеличивать выделение памяти для кеша. Со временем оно становится все выше и выше. Ниже мой класс таймера, который является корнем проблемы. Что я делаю не так?

class TestCountDownTimer(millisInFuture: Long, countDownInterval: Long,
                               private val foregroundService: ForegroundService) : CountDownTimer(millisInFuture, countDownInterval) {


    private val notificationBuilder = NotificationCompat.Builder(foregroundService, Application.CHANNEL_ID).apply {
        val notificationIntent = Intent(foregroundService, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(foregroundService, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)

        setContentTitle("test")
        setSmallIcon(R.drawable.app_icon)
        setContentIntent(pendingIntent)
        setOngoing(true)
    }

    private val countdownIntent: Intent = Intent().apply { action = KEY_BROADCAST_COUNTDOWN }
    private val notificationManager = foregroundService.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    private var isFirstTick = true


    override fun onTick(millisUntilFinished: Long) {
        val timeLeftSeconds = truncate(millisUntilFinished.toDouble() / 1000)

        val hours = truncate(timeLeftSeconds / 60 / 60).toInt()
        val minutes = (truncate(timeLeftSeconds / 60) % 60).toInt()
        val seconds = (timeLeftSeconds % 60).toInt()

        val timeString = "${hours}h : ${minutes}m : ${seconds}s"


        notificationBuilder.setContentText(timeString)
        foregroundService.startForeground(1, notificationBuilder.build())

        countdownIntent.putExtra(KEY_COUNTDOWN_VALUE, timeString)
        foregroundService.sendBroadcast(countdownIntent)
    }

    override fun onFinish() {

    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...