Application贴
oncreate中
WorkManager.getInstance(getApplicationContext());
-------------------------
Application中定义方法
@NonNull
@Override
public ConfigurationgetWorkManagerConfiguration() {
return new Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.INFO)
.build();
}
不要忘了menifest文件中定义
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove" />
应用开启地方 贴
Constraints constraints =new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)//不在电量不足时执行
.setRequiresCharging(true)//在充电时执行
.setRequiresStorageNotLow(true)//不在存储容量不足时执行
.setRequiresDeviceIdle(true)//在待机状态执行
.build();
//2.传入参数
Data data =new Data.Builder().putString("demo", "helloworld").build();
//3.构造work
OneTimeWorkRequest httpwork =new OneTimeWorkRequest.Builder(MyListenWorker.class)
.setConstraints(constraints).setInputData(data).build();
//4.放入执行队列
WorkManager.getInstance(this).enqueue(httpwork);
网友评论