Android Periodic Work Request status stuck at Enqueued

I’m working on an Android App to send out notifications when a date stored in a local database is reached. To do this, I’m attempting to use a periodic work request that will call a function to compare the stored date to the current date and notify the user if the date has been reached. However, when checking Background Task Inspector, my Worker is permanently in “Enqueued” status and never runs this function and I’m unsure why.

Creating the work request in MainActivity.kt’s OnCreate() method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
//...
//For WorkManager to manage Notifications
// Create periodic work request
val periodicWorkRequest = PeriodicWorkRequestBuilder<EventCheckWorker>(5, TimeUnit.SECONDS) //Temporarily set to 5 sec. for testing
.build()
// Enqueue periodic work
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
"EventCheckWorker",
ExistingPeriodicWorkPolicy.KEEP,
periodicWorkRequest
)
}
}
</code>
<code>class MainActivity : ComponentActivity() { @RequiresApi(Build.VERSION_CODES.O) override fun onCreate(savedInstanceState: Bundle?) { //... //For WorkManager to manage Notifications // Create periodic work request val periodicWorkRequest = PeriodicWorkRequestBuilder<EventCheckWorker>(5, TimeUnit.SECONDS) //Temporarily set to 5 sec. for testing .build() // Enqueue periodic work WorkManager.getInstance(this).enqueueUniquePeriodicWork( "EventCheckWorker", ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest ) } } </code>
class MainActivity : ComponentActivity() {
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        
    //...

        //For WorkManager to manage Notifications
        // Create periodic work request
        val periodicWorkRequest = PeriodicWorkRequestBuilder<EventCheckWorker>(5, TimeUnit.SECONDS) //Temporarily set to 5 sec. for testing
            .build()

        // Enqueue periodic work
        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
            "EventCheckWorker",
            ExistingPeriodicWorkPolicy.KEEP,
            periodicWorkRequest
        )

    }
}

And EventCheckWorker to check if date has been reached:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class EventCheckWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
Log.d("START doWork", "START doWork")
return try {
// Retrieve all events from Assignment
val assignmentsFlow = AssignmentDatabase.getDatabase(applicationContext).assignmentDao()
.getAllAssignments()
val today = Calendar.getInstance().timeInMillis
runBlocking {
//Converts Flow<List<Assignment>> into List<Assignment>
assignmentsFlow.collect { assignments ->
// Check if any event date is today. If today, send a notification
for (assignment in assignments) {
Log.d("Loop doWork", "Loop doWork " + assignment.id.toString())
if (assignment.dueDate.time <= today) {
sendNotification(applicationContext, assignment.name)
}
}
}
}
Log.d("SUCCESS doWork", "SUCCESS doWork")
Result.success()
} catch (e: Exception) {
// Log the exception
Log.e("EventCheckWorker", "Error processing events", e)
Result.failure()
}
}
}
</code>
<code>class EventCheckWorker(context: Context, params: WorkerParameters) : Worker(context, params) { override fun doWork(): Result { Log.d("START doWork", "START doWork") return try { // Retrieve all events from Assignment val assignmentsFlow = AssignmentDatabase.getDatabase(applicationContext).assignmentDao() .getAllAssignments() val today = Calendar.getInstance().timeInMillis runBlocking { //Converts Flow<List<Assignment>> into List<Assignment> assignmentsFlow.collect { assignments -> // Check if any event date is today. If today, send a notification for (assignment in assignments) { Log.d("Loop doWork", "Loop doWork " + assignment.id.toString()) if (assignment.dueDate.time <= today) { sendNotification(applicationContext, assignment.name) } } } } Log.d("SUCCESS doWork", "SUCCESS doWork") Result.success() } catch (e: Exception) { // Log the exception Log.e("EventCheckWorker", "Error processing events", e) Result.failure() } } } </code>
class EventCheckWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
    override fun doWork(): Result {
        Log.d("START doWork", "START doWork")
        return try {
            // Retrieve all events from Assignment
            val assignmentsFlow = AssignmentDatabase.getDatabase(applicationContext).assignmentDao()
                .getAllAssignments()

            val today = Calendar.getInstance().timeInMillis

            runBlocking {
                //Converts Flow<List<Assignment>> into List<Assignment>
                assignmentsFlow.collect { assignments ->
                    // Check if any event date is today. If today, send a notification
                    for (assignment in assignments) {
                        Log.d("Loop doWork", "Loop doWork " + assignment.id.toString())
                        if (assignment.dueDate.time <= today) {
                            sendNotification(applicationContext, assignment.name)
                        }
                    }
                }
            }

            Log.d("SUCCESS doWork", "SUCCESS doWork")
            Result.success()
        } catch (e: Exception) {
            // Log the exception
            Log.e("EventCheckWorker", "Error processing events", e)
            Result.failure()
        }
    }
}

The Log statements inside EventCheckWorker never appear in Logcat, meaning doWork() is never called. I’m not sure what could be stopping this method from running.

Here’s a view of the Inspector. I see that I have a constraint requiring a network connection and I’m also unsure why that’s there, as I removed the code setting that constraint and re-ran the app. Regardless though, my emulated device is connected to a network so I’d assume this shouldn’t be an issue.

Background Task Inspector output

Any help with this would be greatly appreciated. Thank you.

New contributor

Joshua Luscher 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