android-workmanager
WorkManager vs AlarmManager, what to use depending on the case
Should I use AlarmManager directly for this ? Yes you should. AlarmManager is the best option as far as I know to handle tasks like yours and also is the safer option when dealing with doze mode. Use the first alarm to set the second alarm at a specific time. Should I use WorkManager to … Read more
Asynchronous Worker in Android WorkManager
I used a countdownlatch and waited for this to reach 0, which will only happen once the asynchronous callback has updated it. See this code: public WorkerResult doWork() { final WorkerResult[] result = {WorkerResult.RETRY}; CountDownLatch countDownLatch = new CountDownLatch(1); FirebaseFirestore db = FirebaseFirestore.getInstance(); db.collection(“collection”).whereEqualTo(“this”,”that”).get().addOnCompleteListener(task -> { if(task.isSuccessful()) { task.getResult().getDocuments().get(0).getReference().update(“field”, “value”) .addOnCompleteListener(task2 -> { if (task2.isSuccessful()) … Read more
Check if WorkManager is scheduled already
Update If you need to check already running work manager just because you don’t want duplicate works. You can simply use enqueueUniquePeriodicWork() This method allows you to enqueue a uniquely-named PeriodicWorkRequest, where only one PeriodicWorkRequest of a particular name can be active at a time. For example, you may only want one sync operation to … Read more
Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent, On AlarmPingSender
Add the following to your build.gradle(app) dependencies. dependencies { // For Java implementation ‘androidx.work:work-runtime:2.7.1’ // For Kotlin implementation ‘androidx.work:work-runtime-ktx:2.7.1’ }
How to create a Worker with parameters for in WorkManager for Android?
You can use setInputData method to send data just like Bundle. /*** Logic to set Data while creating worker **/ val compressionWork = OneTimeWorkRequest.Builder(CompressWorker::class.java) val data = Data.Builder() //Add parameter in Data class. just like bundle. You can also add Boolean and Number in parameter. data.putString(“file_path”, “put_file_path_here”) //Set Input Data compressionWork.setInputData(data.build()) //enque worker WorkManager.getInstance().enqueue(compressionWork.build()) /*** … Read more
How to solve ‘Program type already present: com.google.common.util.concurrent.ListenableFuture’?
In my case, I had to add the following configurations to app’s module build.gradle: configurations { all*.exclude group: ‘com.google.guava’, module: ‘listenablefuture’ } It happens because some dependencies use com.google.guava:guava and com.google.guava:listenablefuture together. It causes a dependency conflict.
Android Work Manager vs Services?
WorkManager comes with following features: Provides tasks which can survive process death It can waken up the app and app’s process to do the work thereby guarantees that works will be executed. Allows observation of work status and the ability to create complex chains of work Allows work chaining which allows to segregate big chunk … Read more
Schedule a work on a specific time with WorkManager
Unfortunately, you cannot schedule a work at specific time as of now. If you have time critical implementation then you should use AlarmManager to set alarm that can fire while in Doze to by using setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). You can schedule a work, with onetime initial delay or execute it periodically, using the WorkManager as … Read more