android-workmanager

Android Workmanager PeriodicWorkRequest is not unique

余生长醉 提交于 2019-11-30 15:50:17
For OneTimeWorkRequest we can use WorkContinuation to ensure that if the job is already scheduled we can KEEP or REPLACE it. There is no such option for PeriodicWorkRequest, so every time my main activity is created a new job is created and after a while I get this exception. java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs So I'm trying the following to create a "unique peiodic work" public void schedule(){ Constraints constraints = new Constraints.Builder().setRequiresBatteryNotLow(true).build(); OneTimeWorkRequest zombieSpawnWorker = new OneTimeWorkRequest

Android WorkManager vs JobScheduler

最后都变了- 提交于 2019-11-30 10:39:43
问题 Why do we need the new Android WorkManager if we already have a JobScheduler along with a few nifty backports (AndroidJob and FirebaseJobDispatcher) with the same functionality? Does it have any killer-features or something? Because I don't see anything that makes me want to migrate to the yet another scheduler. 回答1: "WorkManager has a lot of nice features but its main goal is to use the JobScheduler's API on older devices"... Wait, but we already have some backports. What's wrong with them?

Showing detailed progress for running WorkManager workers

感情迁移 提交于 2019-11-30 09:02:36
问题 I want to replace the job scheduling aspect of my existing data syncing system with the new JetPack WorkManager (link to codelabs) component (in a sandbox branch of the app). My existing system works well but some of the new features in WorkManager would come in handy (e.g. chaining). My current system uses a shared LiveData to communicate the progress from a job in progress to any UI element ( RecyclerView in my case) observing on it (I'm actually SwitchMapping in the ViewModel into a list

How do I return a ListenableFuture<Result> with work manager 2.0?

柔情痞子 提交于 2019-11-29 23:54:32
问题 It seems ListenableWorker no longer has the @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) restriction however I can't figure out or find examples on how to properly return a ListenableFuture< Result> in my overriden startWork() function. As far as I can tell, the only option is to return a SettableFuture.create< Result>() but that still requires suppressing the "RestrictedApi" warning/error. Anyone know a simpler way ? Edit: This is the way to do it as far as I've gathered which makes use of

Unable to set a custom worker factory in WorkManager

余生长醉 提交于 2019-11-29 08:06:50
I use this code to set my own worker factory: val daggerWorkerFactory = DaggerWorkerFactory(toInjectInWorker) val configuration = Configuration.Builder() .setWorkerFactory(daggerWorkerFactory) .build() WorkManager.initialize(context, configuration) After this code execution, I can get the WorkManager instance: val workManager = WorkManager.getInstance() The problem is that for every worker created after this point, my factory is never used. The default factory is used instead. I can see in the API documentation that the method "WorkManager.initialize" has a note: Disable androidx.work.impl

How do I run a ListenableWorker work on a background thread?

前提是你 提交于 2019-11-29 05:21:11
Since I need to perform work asynchronously in WorkManager, I need to use the ListenableWorker , which by default runs on the main (UI) thread. Since this work could be a long processing tasks that could freeze the interface, I wanted to perform it on a background thread. In the Working with WorkManager (Android Dev Summit '18) video , the Google engineer showed how to manually configure WorkManager to run works on a custom Executor , so I followed his guidance: 1) Disable the default WorkManager initializer in the AndroidManifest: <provider android:name="androidx.work.impl

Asynchronous Worker in Android WorkManager

纵饮孤独 提交于 2019-11-29 02:58:48
Google recently announced new WorkManager architecture component. It makes it easy to schedule synchronous work by implementing doWork() in Worker class, but what if I want to do some asynchronous work in the background? For example, I want to make a network service call using Retrofit. I know I can make a synchronous network request, but it would block the thread and just feels wrong. Is there any solution for this or it is just not supported at the moment? Per WorkManager docs : By default, WorkManager runs its operations on a background thread. If you are already running on a background

Check if WorkManager is scheduled already

无人久伴 提交于 2019-11-29 02:25:47
How can I check if WorkManager is scheduled already. Here is my code that schedule WorkManager . public static void scheduleWork() { PeriodicWorkRequest.Builder photoCheckBuilder = new PeriodicWorkRequest.Builder(WorkManagerService.class, TIME_INTERVAL_IN_SECONDS, TimeUnit.SECONDS); PeriodicWorkRequest photoCheckWork = photoCheckBuilder.build(); WorkManager instance = WorkManager.getInstance(); if (instance != null) { instance.enqueueUniquePeriodicWork("TAG", ExistingPeriodicWorkPolicy.KEEP , photoCheckWork); } } I call scheduleWork() in onCreate() of my Application class. Even I can check my

Synchronous or Asynchronous Rxjava inside the Worker (from WorkManager component) what's the right choice?

早过忘川 提交于 2019-11-29 02:18:16
问题 I'm new to the new architecture component WorkManager, I do my API calls via Retrofit and RxJava. My use case here is to get new posts from the Backend, then show notification, and update a widget. So the code inside doWork() method from the Worker class, may look like something like this. @NonNull @Override public Result doWork() { AppDependencies appDependencies = new AppDependencies((Application) getApplicationContext()); Repository repository = appDependencies.getRepository(); repository

Proper way to tackle and resolve “Excessive network usage (background)”

冷暖自知 提交于 2019-11-28 21:20:16
Problem Background Currently, we have facing " Excessive network usage (background) " from Android Vital report. Last 30 days is 0.04% , but we're only Better than 9% Last 30 days - 0.04% Benchmark - Better than 9% Since only better than 9% looks like a scary thing. We decide to look into this issue seriously. The app is a note taking app ( https://play.google.com/store/apps/details?id=com.yocto.wenote ), which provides an optional feature - sync to cloud in background after the app close. This is how we perform sync to cloud in background. We use WorkManager . In Application onPause ,