Android WorkManager vs JobScheduler –

Development issue/problem:

Why do we need a new Android WorkManager when we already have JobScheduler and some backports (AndroidJob and FirebaseJobDispatcher) with the same functionality? Is he homicidal or something? Because I don’t see anything that would make me switch to another planner.

How can I solve this problem?

Solution 1:

WorkManager has many interesting features, but the main goal is to use the Task Scheduler API on older devices….. exploit it. Wait, but we already have some back sports. What’s wrong with them? To bring him down:

  1. FireaseJobDispatcher is good, but we need Google Play to program the work, which is not good if we focus on China, for example.
  2. Evernote’s AndroidJob is a great backdoor with lots of features. Imho, it was the best choice for planning any job. But now the latest version of the library uses the above mentioned WorkManager under the hood. And unfortunately, sooner or later the library will be obsolete:

When you start a new project, use WorkManager instead of this library. You should also start migrating code from this library to WorkManager. At some point, this library will be obsolete.

They suggest switching to WorkManager because it offers more functionality and also gives us a brief comparison:

—————— ———– ———– Accurate work Yes No Ephemeral work Yes No Logbook Yes No Yes Chain work Yes No Yes Continued work Yes No Continued work Yes Yes Continued work Yes Yes Continued work

Imo, the last 3 functions are very useful and are only supported by WorkManager. So the answer to my last question is this: Yeah, he’s got some characteristics of the killer:

  • Google Play is not mandatory
  • Request for
  • Network
  • Opportunist

Learn more about WorkManager in this lecture by Sumir Kataria.

P.S. If anyone knows why FirebaseJobDispatcher is actively supported by Google engineers and has not been written off, please report it in the comments below.

Solution 2:

WorkManager seems to be Google’s answer to Evernote’s job library on Android, but with some improvements. It uses JobScheduler, Firebase JobDispatcher and AlarmManager and Android job depending on the API level of the device. Their use of labels is about the same, and the allocation of tasks/work conditions is quite similar.

Two qualities that fascinate me: the ability to work in a chain and the ability to deal with limitations in an opportunistic way. The first one would split the work (jobs) and make it more modular for me. And with more modular work, each job can have fewer constraints, increasing the chance that it will end earlier (opportunistic). In this way, most of the processing work can be done before the constraints of the network are imposed.

So, if you are satisfied with your current implementation of the planner and the two functions I mentioned do not add value, I do not see much benefit in simply making the change. But if you’re writing something new, it’s probably worth using WorkManager.

Solution 3:

WorkManager uses the JobScheduler service to plan jobs. If the JobScheduler is not supported by the device, it uses Firebase’s JobDispatcher service.
If the Firebase JobDispatcher is not available on the device, AlarmManager and BroadcastReceiver are used.

So with WorkManager, you don’t have to worry about backward compatibility. You can also define the restrictions that must be met to execute a command, for example B. Defining network restrictions, battery level, charge level, and memory level.

It allows you to link tasks and pass arguments to the task.

http://www.zoftino.com/scheduling-tasks-with-workmanager-in-android

Solution 4:

In the first place, the WorkManager is used for tasks that can be postponed and whose execution must be guaranteed. Because of its backward compatibility, the JobScheduler only works with API 23+. So you don’t have to worry about backward compatibility, WorkManager does it for you :-.

Give here a description of the image

Characteristics of the manager’s work

  • Guaranteed and limited performance
  • Limit the background of the compliant system
  • Contested, you can check the status, i.e. failure, success, and so on.
  • Chains, e.g. B. Function A depends on function B -> calendar
  • Able to run at best as soon as the conditions are met without the need for the Job Scheduler to intervene, i.e. wake up the request or wait for the JobSheduler to stack the job while your process is running and being executed.
  • Backward compatible, with or without Google Play.

WorkManager offers compatibility with the Layer 14 API. The WorkManager selects the appropriate method for scheduling a background task based on the API level of the device. It can use JobScheduler (from API 23) or a combination of AlarmManager and BroadcastReceiver.

A long architecture under the bonnet

Give here a description of the image

Solution No 5:

The WorkManager is based on the JobScheduler and the AlarmManager. WorkManager selects the appropriate APIs based on conditions such as the API level of the user’s device.

WorkManager is a simple but incredibly flexible library that offers many additional benefits. Including:

Supports both one-time and recurring asynchronous tasks.
-Supports constraints such as network conditions, storage space and load operations -Status
-Chain’s complex workloads, including parallel operations.
-The result of one work order is used as input for the next one.
-Compatible with API level up to API level 14.
– Works with or without Google Play services.
-Best practices for the health of the system.
-LiveData to easily view the status of applications in the user interface.

Solution No 6:

It is recommended to place all previous background tasks, including FirebaseJobDispatcher, GcmNetworkManager and Job Scheduler, in Google WorkManger documents.

See: https://developer.android.com/topic/libraries/architecture/workmanager

Solution No 7:

In my tests JobScheduler was able to keep the service running after the user closed my application, but I couldn’t find a way to do this with WorkManager.

I tested on a Nexus 5X with Android 8.0.0 Oreo (API 26). I may have been lucky to keep this device/OS combination working after I left the app. I think this may be device specific, as I read in this answer https://stackoverflow.com/a/52605503/2848676 on Android. Does the WorkManager work when the application is closed? Visit dontkillmyapp.com for a complete list of OEM behaviors.

NOTE: I noticed that when the user closed the application, it took a few seconds or even minutes for the JobService to restart.

To enable JobScheduler to run a service that survives the death of the application, create the service as follows:

JobScheduler jobScheduler = (JobScheduler)applicationContext.getSystemService (Context.JOB_SCHEDULER_SERVICE);
Component name component name component name = new component name (applicationContext, ScannerAsJobService).class);
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, component name)
.setPersisted(true) // restart on restart
.setMinimumLatency(1)
.setOverrideDeadline(1)
.build() ;
int result = jobScheduler.schedule(jobInfo) ;

ScannerAsJobService updates the notification with current information, especially if the service is in the background (which I specify because there are different definitions of the background, but this shows accurately whether the application has stopped working) :

import android.app.ActivityManager;
import android.app.NotificationChannel;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.JobParameters;
import android.app.JobService;
import android.content.Context;
import android.content.intentionally;
import android.graphics.infent;
import android.os.Build;
import android.os.Handler ;

import androidx.annotation.RequiresApi ;

import java.util.date;
import java.util.Timer;
import java.util.TimerTask ;

The public class ScannerAsJobService extends JobService {
private static end string TAG = ScannerAsJobService ;

private static lock String NOTIFICATION_CHANNEL_ID = ll_notification_channel ;

last HandlerHandler work = new Handler() ;
RunningableRunable work ;

@Check
public boolean onStartJob (JobParameter parameter) {
workRunnable = new Runnable() {
@Check
public void run() {
// See https://stackoverflow.com/Questions/45692181/android-job-scheduler schedule-job-to-execution-immediaty-and-exactly-once
(new TimerTask())).schedule(new TimerTask() {
@Check
public void run() {
String message = time : + (new Date())).toString() + background : + appInBackground() ;

// https://stackoverflow.com/a/32346246/2848676
Intent = new intent() ;

PendingIntent = PendingIntent.getActivity (ScannerAsJobService.this, 1, intent, 0) ;

Notification.Builder builder = new Notification.Builder (ScannerAsJobService.this) ;

builder.setAutoCancel(false);
builder.setTicker(my ticker info);
builder.setContentTitle(my table of contents);
builder.setContentText(message);
builder.setSmallIcon(R.drawable).my_icon);
builder.setContentIntent(pendingIntent);
// builder.setOngoing(true); // optionally no comment to prevent the user from deleting the
builder notification.setSubText(my subtext); //API Level 16
builder.setNumber(100);
as (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupNotificationChannel(NOTIFICATION_CHANNEL_ID);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
builder.build() ;

NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE) ;
manager.notify(93423, builder.build()) ;

Log.d(TAG + Message) ;

}
}, 1 * 1000, 5 * 1000) ;

jobafgewerkt(params, where) ;
}
} ;
workHandler.post(workRunnable) ;

// return true to tell Android that runRunnable keeps running in the background
return true;
}

// https://stackoverflow.com/a/45692202/2848676
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupNotificationChannel(String channelId) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE) ;

// Understandable channel name for the user.
Character string name = getString(R.string.my_channel_name) ;

// Description of the channel visible to the user.
String description = getString(R.string.my_channel_name) ;

int importance = NotificationManager.IMPORTANCE_LOW ;

NotificationChannel mChannel = new NotificationChannel (channelId, name, importance) ;

// Set up a notification channel.
mChannel.setDescription ;

mChannel.enableLights(true);
// Set the notification color for notifications on this channel
// if the device supports this function.
mChannel.setLightColor (Color.RED) ;

mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}) ;

mNotificationManager.createNotificationChannel(mChannel);
}

/**
* Source: https://stackoverflow.com/a/39589149/2848676
* @return true when the app is in the background, false else
*/
private boolean appIsInBackground() {
ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
boolean isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
return isInBackground;
}

@Override
public boolean onStopJob(JobParameters) {
// return true Function schemes for repetitions (we use it to implement periodicity since
// JobInfo.Builder.setPeriodic() did not work,
gives true;
}
}.

Good luck!

Related Tags:

work manager android example github,workmanager vs alarmmanager,workmanager android medium,android job scheduler every 10 minutes,android run task at specific time,workmanager not working when app is killed,workmanager screen off,workmanager vs syncadapter,jobscheduler in android,work manager example in android,firebase jobdispatcher,workmanager vs foreground service,workmanager vs services android,android manager vs service,android autostart permission,workmanager network connection,periodic workmanager android example,workmanager android example kotlin,rxworker example android,android-workmanager notification,alarmmanager example android,gcm network manager vs job scheduler,workmanager doze mode,firebase job dispatcher vs job scheduler,workmanager android,android jobscheduler,android workmanager background service,android workmanager vs service,workmanager android stackoverflow,android-workmanager location

Leave a Reply

Your email address will not be published. Required fields are marked *