Operation of WorkManager in the timer app

I want to develop an app that runs a timer on Android.

I would like it to function similarly to the default timer app on smartphones.

When the timer is running, it should display the decreasing time in the UI, and it should continue to function correctly even if the app is closed and reopened.

However, I am not sure how to maintain the state when the app is closed and reopened.

Initially, I implemented the timer app by setting the time using AlarmManager so that an alarm would ring at the exact time.

However, when the app is closed and reopened, I couldn’t check the alarm registered with AlarmManager. To address this, I used a separate DataStore to save the state, but I thought this method wasn’t ideal. Additionally, with this method, I couldn’t display the decreasing timer state in the UI.

So currently, I am using WorkManager. I used setForeground to calculate the remaining time in the background.

There were unresolved issues while implementing the timer app using WorkManager.

  1. I implemented it to run for a long time using setForeground, but if the user arbitrarily stops the background service, the timer stops, and when the app is restarted, the work starts from the beginning.

  2. setBackoffCriteria is set to default, so I don’t know how to prevent retries.

  3. In the case of the default timer app, the timer works normally even if the background service is arbitrarily stopped. I don’t know how they implemented it. Is there really a way? I’m curious how they did it.

  4. For a timer app, I wonder if implementing it through WorkManager is a good pattern and if it is a commonly used structure.

Here is my current code.

class TimerWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {

    override suspend fun doWork(): Result {
        val minutes = inputData.getInt("minutes", 0)

        val notification = NotificationCompat
            .Builder(applicationContext, NotificationHelper.CHANNEL_ID_TIMER_ALARM)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Backgrounding...")
            .setOngoing(true)
            .build()

        val info = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            ForegroundInfo(999, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
        } else {
            ForegroundInfo(999, notification)
        }
        setForeground(info)

        return try {
            val endTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(minutes.toLong())

            while (System.currentTimeMillis() < endTime) {
                Thread.sleep(100)
                println(endTime - System.currentTimeMillis())
            }
            Result.success()
        } catch (e: InterruptedException) {
            Result.failure()
        }
    }
}

fun scheduleTimerWork(context: Context, minutes: Int) {
    val timerWorkRequest = OneTimeWorkRequestBuilder<TimerWorker>()
        .setInputData(workDataOf("minutes" to minutes))
        .build()

    WorkManager.getInstance(context).enqueue(timerWorkRequest)
}

New contributor

placeCover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật