WorkManager是什么
WorkManager可以管理和执行一些后台任务,当条件达到时保证任务,在应用启动的情况下可以正常执行,在应用没启动的情况下也可以正常执行。
WorkManager的实现方案
1.WorkManager执行任务时应用程序还在运行,WorkManager会在应用程序进程中创建一个的新线程并在新线程中运行任务;
2.WorkManager执行任务时应用程序没有运行,WorkManager会选择适当的方式来安排后台任务 - 根据设备API级别和包含的依赖项,WorkManager可能会使用JobScheduler,Firebase JobDispatcher或AlarmManager。
WorkManager相关类
WorkManager: 按照工作请求的约束条件调度和管理工作请求。
Worker:我们需要执行的任务,这是一个抽象类我们需要继承它在 doWork()方法中实现我们自己的业务逻辑。
WorkRequest: 工作请求,每个Worker想要被执行都需要想WorkManager发出一个WorkRequest,同时在WorkRequest中指定Worker执行的条件。
WorkStatus:WorkManager中Worker的状态信息。
WorkManager工作流程
任务类型有:典型任务,重复任务,链式任务,这三种任务都可以指定约束条件。
典型任务例子:
1.声明一个需要执行任务的类
public class SimpleWorker extends Worker {
@NonNull
@Override
public Result doWork() {
//TODO 需要在任务中执行的业务逻辑
Log.i("SimpleWorker", "doWork");
return Result.SUCCESS;
}
}
2.在Activity中运行一次任务
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SimpleWorker.class).build();
WorkManager.getInstance().enqueue(workRequest);
WorkManager的使用
WorkManager requires compileSdk version 28 or higher.
1.引入相关依赖到项目中
dependencies {
def work_version = "1.0.0-alpha09"
implementation "android.arch.work:work-runtime:$work_version" // use -ktx for Kotlin
// optional - Firebase JobDispatcher support
implementation "android.arch.work:work-firebase:$work_version"
// optional - Test helpers
androidTestImplementation "android.arch.work:work-testing:$work_version"
}
2.创建MViewModel类继承系统的AndroidViewModel(ViewModel的子类AndroidViewModel)
package com.architecture.demo;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.architecture.demo.util.CONSTANT;
import com.architecture.demo.util.Utils;
import com.architecture.demo.workManager.CollectAppInfoWorker;
import com.architecture.demo.workManager.SimpleWorker;
import com.architecture.demo.workManager.SimpleWorker2;
import com.architecture.demo.workManager.SimpleWorker3;
import com.architecture.demo.workManager.SimpleWorker4;
import com.architecture.demo.workManager.SimpleWorker5;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.State;
import androidx.work.WorkContinuation;
import androidx.work.WorkManager;
import androidx.work.WorkStatus;
public class WorkerActivity extends AppCompatActivity {
public static String WORKER_TAG = "worker_tag";
UUID mId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_worker);
}
public void deleteFile(View view) {
boolean delete = Utils.delete(CONSTANT.PATH);
if (delete) Utils.showToast(getApplicationContext(), "删除成功!");
}
public void startOneTimeWorkRequest(View view) {
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(CollectAppInfoWorker.class).build();
WorkManager.getInstance().enqueue(workRequest);
}
public void startPeriodicWorkRequest(View view) {
PeriodicWorkRequest workRequest = new PeriodicWorkRequest
.Builder(SimpleWorker.class, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS)
.addTag(WORKER_TAG)
.build();
mId = workRequest.getId();
WorkManager.getInstance().enqueue(workRequest);
LiveData<List<WorkStatus>> statusesByTag = WorkManager.getInstance().getStatusesByTag(WORKER_TAG);
statusesByTag.observe(this, new Observer<List<WorkStatus>>() {
@Override
public void onChanged(@Nullable List<WorkStatus> workStatuses) {
Log.i(CONSTANT.TAG_WORKER, "onChanged");
if (workStatuses == null) return;
for (WorkStatus workStatus : workStatuses) {
UUID id = workStatus.getId();
Log.i(CONSTANT.TAG_WORKER, "onChanged_id:" + id);
if (mId != id) break;
Data outputData = workStatus.getOutputData();
String data = outputData.getString("data");
Log.i(CONSTANT.TAG_WORKER, "data:" + data);
State state = workStatus.getState();
boolean finished = state.isFinished();
Log.i(CONSTANT.TAG_WORKER, "finished:" + finished);
}
}
});
}
public void cancelPeriodicWorkRequest(View view) {
WorkManager.getInstance().cancelAllWorkByTag(WORKER_TAG);
/*//可以通过id或者tag删除worker
if (mId != null) {
WorkManager.getInstance().cancelWorkById(mId);
mId = null;
}*/
}
public void startContinuation(View view) {
OneTimeWorkRequest workRequestA = new OneTimeWorkRequest.Builder(SimpleWorker.class).build();
OneTimeWorkRequest workRequestB = new OneTimeWorkRequest.Builder(SimpleWorker2.class).build();
OneTimeWorkRequest workRequestC = new OneTimeWorkRequest.Builder(SimpleWorker3.class).build();
WorkManager.getInstance()
.beginWith(workRequestA)
.then(workRequestB)
.then(workRequestC)
.enqueue();
}
public void startMultiContinuation(View view) {
OneTimeWorkRequest workRequestA = new OneTimeWorkRequest.Builder(SimpleWorker.class).build();
OneTimeWorkRequest workRequestB = new OneTimeWorkRequest.Builder(SimpleWorker2.class).build();
OneTimeWorkRequest workRequestC = new OneTimeWorkRequest.Builder(SimpleWorker3.class).build();
OneTimeWorkRequest workRequestD = new OneTimeWorkRequest.Builder(SimpleWorker4.class).build();
OneTimeWorkRequest workRequestE = new OneTimeWorkRequest.Builder(SimpleWorker5.class).build();
WorkContinuation chain1 = WorkManager.getInstance()
.beginWith(workRequestA)
.then(workRequestB);
WorkContinuation chain2 = WorkManager.getInstance()
.beginWith(workRequestC)
.then(workRequestD);
WorkContinuation chain3 = WorkContinuation
.combine(chain1, chain2)
.then(workRequestE);
chain3.enqueue();
}
public void startConstraints(View view) {
int type = Utils.getConnectedType(getApplicationContext());
Log.i(CONSTANT.TAG_WORKER, "NetworkInfo:" + type);
Constraints.Builder constraintsBuilder = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.NOT_REQUIRED)//指定任务执行时的网络状态,网络链接时候运行,默认NOT_REQUIRED
.setRequiresBatteryNotLow(false)//指定设备电池电量低于阀值时是否启动任务,默认false 执行任务时电池电量不能偏低
.setRequiresStorageNotLow(false)//指定设备储存空间低于阀值时是否启动任务,默认false 设备储存空间足够时才能执行
.setRequiresCharging(false);//指定设备在充电时是否启动任务,默认false 在设备充电时才能执行任务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
constraintsBuilder.setRequiresDeviceIdle(false);//指明设备为空闲时是否启动任务。 设备空闲时才能执行
}
Constraints constraints = constraintsBuilder.build();
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SimpleWorker.class).setConstraints(constraints).build();
WorkManager.getInstance().enqueue(workRequest);
}
}
通过以上步骤既可完成WorkManager的简单使用。
Demo地址:
https://github.com/CongYiMing/ArchitectureDemo