I have a chance to use Android's WorkManager, and I will write down what I have researched as a memorandum. This article describes the basic usage of WorkManager. The content is simple for beginners.
According to the Official Page, there is the following description.
The WorkManager API makes it easy to schedule deferrable asynchronous tasks that are required to run even if the app exits or the device restarts.
in short,
You can see that it is an API. Previously, it was implemented using JobScheduler and BroadcastReceiver. It seems that this has become easier. As a detailed function, it seems that you can add restrictions on network and charging status, and specify the execution timing. Also, it seems that it is implemented in an appropriate way depending on the API level, so we can implement it without worrying about the API level. This is a pretty useful API.
This sample app creates an app that outputs a notification (log this time) 10 seconds after the app is closed.
I will try this. Moreover, Kotlin is currently the mainstream on Android, but this time I would like to implement it in Java.
Let's implement it now.
The flow is such that the task is implemented in the class that inherits Worker
, the constraint is added to WorkRequest
, and the submission is submitted to WorkManager
.
This time, I will explain with the following flow.
First, import the WorkManager library into your Android project. Add the following dependency to your app's build.gradle
file.
dependencies {
implementation "androidx.work:work-runtime:2.4.0"
//···(abridgement)···
}
Tasks are implemented by inheriting the Worker
class. Override the doWork ()
method.
public class SampleWorker extends Worker { //・ ・ ・(1)
private static String TAG = SampleWorker.class.getName();
public SampleWorker(Context context, WorkerParameters params) {
super(context, params);
}
@Override
public Result doWork() { //・ ・ ・(2)
Log.d(TAG, "Performing background tasks");
return Result.success(); //・ ・ ・(3)
}
}
Return value | Overview |
---|---|
Result.success() | When the task completes successfully |
Result.failure() | When a task fails |
Result.retry() | When you need to retry the task later |
To execute a task, use the WorkRequest
class.
Whereas Worker
defines a task, WorkRequest
defines how and when to perform the task. There are ʻOneTimeWorkRequest that executes the task only once and
PeriodicWorkRequest that executes it regularly. This time, we will use ʻOneTimeWorkRequest
which is executed only once.
public class SampleActivity extends AppCompatActivity {
//···(abridgement)···
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { //・ ・ ・(1)
if (keyCode == KeyEvent.KEYCODE_BACK) {
//・ ・ ・(2)
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SampleWorker.class)
.setInitialDelay(10, TimeUnit.SECONDS) //・ ・ ・(3)
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(workRequest);
//・ ・ ・(4)
}
return super.onKeyDown(keyCode, event);
}
WorkRequest
, set it in the WorkManager
using the ʻenqueue ()` method.First, launch the app. Then press the back key to return to the home screen (to background) If the log is output after 10 seconds, it is successful!
Did the sample work? It may be difficult to understand at first, but you will get used to it after implementing it several times. WorkManager can set various restrictions (charging, network connection status, etc.). You can also do it on a regular basis. I would like to write an article about this in the near future.
see you!
Recommended Posts